简体   繁体   English

如何解决错误:freemarker.template.TemplateNotFoundException 并使用 Spring Boot 在 FreeMarker 中指定加载模板文件的自定义路径

[英]How to resolve error: freemarker.template.TemplateNotFoundException and specify custom path for load template file in FreeMarker with Spring Boot

Resolve error:解决错误:

freemarker.template.TemplateNotFoundException: Template not found for name . freemarker.template.TemplateNotFoundException:找不到 name 的模板。 The name was interpreted by this TemplateLoader: LegacyDefaultFileTemplateLoader(baseDir="", canonicalBasePath="" Warning: The "template_loader" FreeMarker setting wasn't set (Configuration.setTemplateLoader), and using the default value is most certainly not intended and dangerous, and can be the cause of this error.该名称由该 TemplateLoader 解释:LegacyDefaultFileTemplateLoader(baseDir="", canonicalBasePath="" 警告:未设置“template_loader”FreeMarker 设置 (Configuration.setTemplateLoader),并且使用默认值肯定不是有意的和危险的,并且可能是导致此错误的原因。

To specify a custom path from which to read the template file in Java Spring Boot just create a method like this that creates a custom FileTemplateLoader:要指定从 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;
    }

rather than:而不是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM