【问题标题】:How to resolve error: freemarker.template.TemplateNotFoundException and specify custom path for load template file in FreeMarker with Spring Boot如何解决错误:freemarker.template.TemplateNotFoundException 并使用 Spring Boot 在 FreeMarker 中指定加载模板文件的自定义路径
【发布时间】:2021-12-19 18:22:58
【问题描述】:

解决错误:

freemarker.template.TemplateNotFoundException:找不到模板 姓名 。该名称由此 TemplateLoader 解释: LegacyDefaultFileTemplateLoader(baseDir="", canonicalBasePath="" 警告:未设置“template_loader”FreeMarker 设置 (Configuration.setTemplateLoader),并且使用默认值最多 当然不是故意和危险的,并且可能是造成这种情况的原因 错误。

【问题讨论】:

    标签: java spring-boot freemarker


    【解决方案1】:

    要在 Java Spring Boot 中指定从其读取模板文件的自定义路径,只需创建一个类似这样的方法来创建自定义 FileTemplateLoader:

    public freemarker.template.Configuration getConfiguration() throws IOException {
        FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(new File(templateBasePath));
        freemarker.template.Configuration config = new freemarker.template.Configuration();
        config.setTemplateLoader(fileTemplateLoader);
        return config;
    }
    

    而不是:

    @Bean
    public FreeMarkerConfigurationFactoryBean freemarkerConfiguration() {
        FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
        bean.setTemplateLoaderPath("classpath:/templates/");
        return bean;
    }
    

    【讨论】:

    • Freemarker,当检测到是自动配置的,所以你不应该需要这样的东西。可以在application.properties中指定路径。
    猜你喜欢
    • 2018-10-22
    • 2021-11-27
    • 2013-10-21
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多