在java中加载freemarker模板时FileNotFoundException

前端之家收集整理的这篇文章主要介绍了在java中加载freemarker模板时FileNotFoundException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在加载freemarker模板时,即使该模板实际存在于路径中,我也收到一个找不到异常的文件.

更新:这是作为一个webservice运行.它会根据搜索查询将xml返回给客户端.当我从另一个java程序(从静态主机)调用它时,模板加载成功.但是当客户端请求xml时,发生FileNotFoundException.

操作系统:Windows 7
文件绝对路径:C:/ Users / Jay / workspace / WebService / templates /

这是我的代码

private String templatizeQuestion(QuestionResponse qr) throws Exception
{
    SimpleHash context = new SimpleHash();
    Configuration config = new Configuration();

    StringWriter out = new StringWriter();

    Template _template = null;

    if(condition1)
    {           
        _template = config.getTemplate("/templates/fibplain.xml");
    } 
    else if(condition2)
    {
        _template = config.getTemplate("/templates/mcq.xml");
    }
    context.put("questionResponse",qr);
    _template.process(context,out);

    return out.toString();
 }

错误堆栈:

java.io.FileNotFoundException: Template /templates/fibplain.xml not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:495)
at freemarker.template.Configuration.getTemplate(Configuration.java:458)
at com.hm.newAge.services.Curriculum.templatizeQuestion(Curriculum.java:251)
at com.hm.newAge.services.Curriculum.processQuestion(Curriculum.java:228)
at com.hm.newAge.services.Curriculum.processQuestionList(Curriculum.java:210)
at com.hm.newAge.services.Curriculum.getTest(Curriculum.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

解决方法

FreeMarker模板路径由TemplateLoader对象解析,该对象应在Configuration对象中指定.您指定为模板路径的路径由TemplateLoader解释,通常相对于某种基本目录(即使以/开头),因此也称为模板根目录.在你的例子中,你没有指定任何TemplateLoader,所以你使用的是默认的TemplateLoader,它只是向后兼容的,几乎没用(也是危险的).所以,做这样的事情:
config.setDirectoryForTemplateLoading(new File(
    "C:/Users/Jay/workspace/WebService/templates"));

接着:

config.getTemplate("fibplain.xml");

请注意,模板前缀不存在,因为模板路径相对于C:/ Users / Jay / workspace / WebService / templates. (这也意味着模板不能用../-s退出,这可能对安全性很重要.)

您也可以从SerlvetContext,“类路径”等加载模板,而不是从真实目录加载.这一切都取决于您选择的TemplateLoader.

参见:http://freemarker.org/docs/pgui_config_templateloading.html

更新:如果您获取FileNotFoundException而不是TemplateNotFoundException,那么现在是将FreeMarker升级到至少2.3.22的时候了.它还会提供更好的错误消息,就像您使用默认的TemplateLoader的典型错误一样,它会在错误消息中告诉您.较少浪费开发人员时间.

原文链接:https://www.f2er.com/java/122756.html

猜你在找的Java相关文章