在Dropwizard Intellij中找不到Freemarker模板

时间:2018-02-27 19:30:22

标签: java freemarker dropwizard dropwizard-templates

我正在尝试设置一个自由标记模板来显示SQL查询的结果。我遇到了Intellij报告无法找到模板的问题:

DEBUG [2018-02-27 18:52:31,623] freemarker.cache: Couldn't find template in cache for "com/example/www/resources/template.ftl"("en_US", ISO-8859-1, parsed); will try to load it.
DEBUG [2018-02-27 18:52:31,624] freemarker.cache: TemplateLoader.findTemplateSource("com/example/www/resources/template_en_US.ftl"): Not found
DEBUG [2018-02-27 18:52:31,626] freemarker.cache: TemplateLoader.findTemplateSource("com/example/www/resources/template_en.ftl"): Not found
DEBUG [2018-02-27 18:52:31,627] freemarker.cache: TemplateLoader.findTemplateSource("com/example/www/resources/template.ftl"): Not found
DEBUG [2018-02-27 18:52:31,661] org.eclipse.jetty.server.handler.gzip.GzipHttpOutputInterceptor: org.eclipse.jetty.server.handler.gzip.GzipHttpOutputInterceptor@f62788 exclude by status 404
DEBUG [2018-02-27 18:52:31,662] org.eclipse.jetty.server.HttpChannel: sendResponse info=null content=HeapByteBuffer@d1e062[p=0,l=248,c=32768,r=248]={<<<<html>\n<head>\n<me.../body>\n</html>\n>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00} complete=true committing=true callback=Blocker@4fab4a{null}
DEBUG [2018-02-27 18:52:31,662] org.eclipse.jetty.server.HttpChannel: COMMIT for /address/3 on HttpChannelOverHttp@a67c6b{r=1,c=true,a=DISPATCHED,uri=//localhost:8080/address/3}
404 Not Found HTTP/1.1
Date: Tue, 27 Feb 2018 18:52:31 GMT
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1

当我的app类中的main方法设置如下时,就会发生这种情况:

public class AppApplication extends Application<AppConfiguration> {

     public static void main(final String[] args) throws Exception {

         new AppApplication().run(args);
}

在此设置下,导航到返回地址数据库的URL会在浏览器中返回404错误,上面的日志会以调试模式返回。

在Dropwizard中使用Freemarker的其他研究使我在应用程序的main方法中设置了一个Configuration实例,用setDirectoryForTemplateLoading查找模板,指向路径,并使用getTemplate返回模板文件本身:

public class AppApplication extends Application<AppConfiguration> {

    public static void main(final String[] args) throws Exception {

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);

        cfg.setDirectoryForTemplateLoading(new File("C:\\IdeaProjects\\App\\src\\main\\resources"));
        cfg.getTemplate("template.ftl");

        cfg.setDefaultEncoding("UTF-8");

        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);

        cfg.setLogTemplateExceptions(false);

        cfg.setWrapUncheckedExceptions(true);

        new AppApplication().run(args);
    }

但是,转到URL会返回相同的错误。在这种情况下唯一可辨别的区别是,当服务器启动时,错误信息将打印在控制台顶部:

14:09:24.891 [main] DEBUG freemarker.cache - Couldn't find template in cache for "template.ftl"("en_US", UTF-8, parsed); will try to load it.
14:09:24.907 [main] DEBUG freemarker.cache - TemplateLoader.findTemplateSource("template_en_US.ftl"): Not found
14:09:24.907 [main] DEBUG freemarker.cache - TemplateLoader.findTemplateSource("template_en.ftl"): Not found
14:09:24.907 [main] DEBUG freemarker.cache - TemplateLoader.findTemplateSource("template.ftl"): Found
14:09:24.922 [main] DEBUG freemarker.cache - Loading template for "template.ftl"("en_US", UTF-8, parsed) from "C:\\IdeaProjects\\App\\src\\main\\resources\\template.ftl"

最后,它显示它是从资源文件夹加载模板;但是,尝试运行我定义的getter方法稍后会在控制台中返回相同的错误。有人能给我一些见解吗?我不认为这是我的代码的问题,而是.ftl文件没有重新发布到Intellij。

编辑:这是我的View Bundle,初始化。:

bootstrap.addBundle(new ViewBundle<AppConfiguration>() {
            public Map<String, Map<String, String>> getViewConfiguration(AppConfiguration configuration) {
                return configuration.getViewRendererConfiguration();
            }
        });

0 个答案:

没有答案
相关问题