简体   繁体   English

Spring Boot:Freemarker 找不到模板

[英]Spring boot: template not found with Freemarker

I have a spring boot application(version 2.2.5.RELEASE) which is packaged as a jar file:我有一个打包为 jar 文件的 Spring Boot 应用程序(版本 2.2.5.RELEASE):

I am using Freemarker(version 2.3.29) to generate an html template please find my config below:我正在使用 Freemarker(版本 2.3.29)生成一个 html 模板,请在下面找到我的配置:

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

            return bean;
        }

Please find my pom.xml file that included ftl file extension:请找到包含 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>

When I packaged my jar I can find the file at the following path: BOOT-INF/classes/templates/file12.ftl当我打包 jar 时,我可以在以下路径找到该文件:BOOT-INF/classes/templates/file12.ftl

Please find below the code I am generating the html file:请在我生成 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();

        }

.... When I execute the jar from the following command: .... 当我从以下命令执行 jar 时:

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

The following exception is displayed:显示以下异常:

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

When I execute same code base in integration test on eclipse it works fine.当我在 eclipse 上的集成测试中执行相同的代码库时,它工作正常。

Any idea what i am missing here please?知道我在这里缺少什么吗?

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

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

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

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