freemarker错误加载模板

时间:2016-10-20 21:56:23

标签: java freemarker

获得以下异常,是否有人有此解决方案 也如mongodb教程所示,它实际上是.ftl,这里我给了html文件 我无法创建.ftl文件

package com.mongodb;

    import freemarker.template.Configuration;
    import freemarker.template.Template;

    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;

    /**
     * Created by tadoori on 10/20/2016.
     */
    public class HelloworldFreemarkerStyle {

        public static void main(String[] args) {
            Configuration configuration = new Configuration();
            configuration.setClassForTemplateLoading(HelloworldFreemarkerStyle.class, "/");

            try {

                Template helloTemplate = configuration.getTemplate("hello.html");
                StringWriter writer = new StringWriter();
                Map<String, Object> helloMap = new HashMap<String, Object>();
                helloMap.put("name", "Freemark");

                helloTemplate.process(helloMap, writer);

                System.out.println(writer);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

以下是运行java文件

java.io.FileNotFoundException: resources does not exist.
    at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:125)
    at java.security.AccessController.doPrivileged(Native Method)
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:122)
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:108)
    at com.mongodb.HelloworldFreemarkerStyle.main(HelloworldFreemarkerStyle.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

1 个答案:

答案 0 :(得分:0)

我在我的测试项目中尝试了这个代码,创建了与Idea2016(freemarker v.2.3.23)并且完美的工作。

DetailViewController

文件hello.html位于项目目录root。