0

我正在尝试设置一个 freemarker 模板来显示 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

当我的应用程序类中的主要方法设置如下时发生这种情况:

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 注册。

编辑:这是我的视图包,在初始化下。:

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

0 回答 0