春季启动:Freemarker找不到模板

时间:2020-03-30 06:54:24

标签: java spring spring-boot spring-batch freemarker

我有一个打包为jar文件的spring boot应用程序(版本2.2.5.RELEASE):

我正在使用Freemarker(版本2.3.29)生成一个html模板,请在下面找到我的配置:

@Configuration
public class EmailFreemarkerConfig {
     @Primary
      @Bean
        public FreeMarkerConfigurationFactoryBean getFreeMarkerConfig() {
            FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
            bean.setTemplateLoaderPath("/templates/");

            return bean;
        }

请找到我的包含ftl文件扩展名的pom.xml文件:

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.csv</include>
                    <include>**/*.vm</include>
                    <include>**/*.jpg</include>
                    <include>**/*.ftl</include>
                </includes>
            </resource>
        </resources>

打包jar时,可以在以下路径中找到文件: BOOT-INF / classes / templates / file12.ftl

请在下面找到我正在生成html文件的代码:

   @Autowired
    private Configuration freemarkerConfig;

.....
try {
            t = freemarkerConfig.getTemplate("file12.ftl");
            html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        } catch (IOException | TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

... 当我从以下命令执行jar时:

java -jar test-batch-0.0.2-SNAPSHOT.jar

显示以下异常:

freemarker.template.TemplateNotFoundException: Template not found for name "file12.ftl"

当我在Eclipse的集成测试中执行相同的代码库时,效果很好。

你知道我在这里想念什么吗?

1 个答案:

答案 0 :(得分:1)

以防万一有人遇到这个问题,我在配置中错过了类路径:

bean.setTemplateLoaderPath("classpath:/templates/");