在 Spring 应用程序中使用 Freemarker 加载 spring.ftl 时出现 FileNotFoundException

问题描述 投票:0回答:1

我陷入了在 Spring 应用程序中集成 Freemarker 的困境。 我有一个负责生成 html 到 pdf 的库,它被注入到我的 web 应用程序中。 spring.ftl 位于我的 lib 模块资源内的 templates 文件夹中

my-lib/src/main/resources/templates/spring.ftl

我定义了一个 FileTemplateLoader 来引用模板文件夹,并为 spring.ftl 添加了 auto_import 属性

当我尝试生成 pdf 文件时,当 Freemarker 配置尝试设置模板加载器时,我收到 FileNotFoundException

配置方法:

private static Configuration getConfig() throws IOException, TemplateException {
        // Create your Configuration instance, and specify if up to what FreeMarker
        // version (here 2.3.32) do you want to apply the fixes that are not 100%
        // backward-compatible. See the Configuration JavaDoc for details.
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);

        // Specify the source where the template files come from. Here I set a
        // plain directory for it, but non-file-system sources are possible too:
        //cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));

        // From here we will set the settings recommended for new projects. These
        // aren't the defaults for backward compatibilty.

        cfg.setDefaultEncoding("UTF-8");

        // Sets how errors will appear.
        // During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // Don't log exceptions inside FreeMarker that it will thrown at you anyway:
        cfg.setLogTemplateExceptions(false);

        // Wrap unchecked exceptions thrown during template processing into TemplateException-s:
        cfg.setWrapUncheckedExceptions(true);

        // Do not fall back to higher scopes when reading a null loop variable:
        cfg.setFallbackOnNullLoopVariable(false);

        cfg.setTemplateLoader(new FileTemplateLoader(new File("templates")));
        Properties settings = new Properties();
        settings.setProperty("auto_import", "/spring.ftl as spring");
        cfg.setSettings(settings);

        return cfg;
    }

错误:

java.io.FileNotFoundException: templates does not exist.
    at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:123) ~[freemarker-2.3.32.jar:2.3.32]
    at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:119) ~[freemarker-2.3.32.jar:2.3.32]
    at java.security.AccessController.doPrivileged(AccessController.java:569) ~[?:?]
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:119) ~[freemarker-2.3.32.jar:2.3.32]
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:98) ~[freemarker-2.3.32.jar:2.3.32]
java spring freemarker
1个回答
0
投票

我使用下面的实现解决了类加载器异常:

cfg.setTemplateLoader(new ClassTemplateLoader(DocGeneratorUtils.class, "/templates"));
Properties settings = new Properties();
settings.setProperty("auto_import", "/spring.ftl as spring");
cfg.setSettings(settings);
© www.soinside.com 2019 - 2024. All rights reserved.