在java中加载freemarker模板时FileNotFoundException [英] FileNotFoundException when loading freemarker template in java

查看:140
本文介绍了在java中加载freemarker模板时FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载freemarker模板时,即使该模板实际存在于路径中,我也收到一个找不到异常的文件。

I get a file not found exception while loading a freemarker template even though the template is actually present in the path.

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

Update: This is running as a webservice. It will return an xml to the client based on a search query. The template loads successfully when i call it from another java program(from static main). But the when the client requests for the xml, FileNotFoundException occurs.

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

OS: Windows 7 Absolute path of file: 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 对象解析,您应该在配置对象中指定。您指定为模板路径的路径由 TemplateLoader 解释,通常相对于某种基本目录(即使以开头) / ),因此也称为模板根目录。在你的例子中,你没有指定任何 TemplateLoader ,所以你使用默认的 TemplateLoader 为了向后兼容,几乎没有用(也是危险的)。所以,做这样的事情:

FreeMarker template paths are resolved by a TemplateLoader object, which you should specify in the Configuration object. The path that you specify as the template path is interpreted by the TemplateLoader, and is usually relative to some kind of base directory (even if it starts with /), that's also called the template root directory for this reason. In your example, you haven't specified any TemplateLoader, so you are using the default TemplateLoader, which is only there for backward-compatibility, and is nearly useless (and also dangerous). So, do something like this:

config.setDirectoryForTemplateLoading(new File(
    "C:/Users/Jay/workspace/WebService/templates"));

然后:

config.getTemplate("fibplain.xml");

请注意, / template 现在,由于模板路径相对于 C:/ Users / Jay / workspace / WebService / templates 。 (这也意味着模板无法从 ../ -s中退出,这对于安全性来说可能很重要。)

Note that the /template prefix is not there now, as the template path is relative to C:/Users/Jay/workspace/WebService/templates. (This also means that the template can't back out of it with ../-s, which can be important for security.)

而不是从真正的目录中加载,还可以从类路径等从 SerlvetContext 加载模板,这一切都取决于

Instead of loading from a real directory, you can also load templates from a SerlvetContext, from the "class path", etc. It all depends on what TemplateLoader you are choosing.

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

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

Update: If you get FileNotFoundException instead of TemplateNotFoundException, it's time to upgrade FreeMarker to at least 2.3.22. It also gives better error messages, like if you do the typical mistake of using the default TemplateLoader, it tells you that right in the error message. Less wasted developer time.

这篇关于在java中加载freemarker模板时FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆