如何从src/main/resources文件夹中读取Freemarker模板文件?

40

如何从我的代码(Spring Boot应用程序)访问存储在src / main / resources文件夹中的freemarker模板(*.ftl)文件?

我尝试了以下方法

freemarker.template.Configuration config = new Configuration();
configuration.setClassForTemplateLoading(this.getClass(), "/resources/templates/");

并且收到以下异常

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

2
如果您使用Spring的开箱即用配置,实际上不需要做任何事情。因为Spring Boot已经为您配置了FreeMarker,包括解析器。 - M. Deinum
2个回答

65

类路径的根目录是src/main/resources,将路径更改为

configuration.setClassForTemplateLoading(this.getClass(), "/templates/");

但如果我们从jar文件运行代码,如何修改代码以适应它?例如:java -jar freemarker.jar,在这个jar文件中,模板文件夹位于该jar包的根目录下。 - Alter Hu
给更多的人提供这里的想法: new ClassTemplateLoader(getClass().getClassLoader(), "templates") new ClassTemplateLoader(getClass(), "/templates") - Alter Hu
@AlterHu,理想情况下,您应该将src/main/resources放在您的jar文件中。 - tugcem
@Biscuit128 完全同意你的评论 :D - Vishrant

6
我遇到了“freemarker.template.TemplateNotFoundException:找不到名称为...的模板”问题。 我的代码是正确的,但我忘记在pom.xml中包含/templates/目录。所以下面的代码为我解决了这个问题。希望这能帮到你。
 AppConfig.java :

    @Bean(name="freemarkerConfiguration")
    public freemarker.template.Configuration getFreeMarkerConfiguration() {
        freemarker.template.Configuration config = new freemarker.template.Configuration(freemarker.template.Configuration.getVersion());
        config.setClassForTemplateLoading(this.getClass(), "/templates/");
        return config;
    }

 EmailSenderServiceImpl.java:

    @Service("emailService")
    public class EmailSenderServiceImpl implements EmailSenderService 
    {
        @Autowired
        private Configuration freemarkerConfiguration;

        public String geFreeMarkerTemplateContent(Map<String, Object> dataModel, String templateName)
        {
            StringBuffer content = new StringBuffer();
            try {
                content.append(FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(templateName), dataModel));
                return content.toString();
            }
            catch(Exception exception) {
                logger.error("Exception occured while processing freeMarker template: {} ", exception.getMessage(), exception);
            }
            return "";
        }
    }


 pom.xml :

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <includes>
                    <include>templates/*.ftl</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/</directory>
                <includes>
                    <include>templates/*.ftl</include>
                </includes>
            </resource>
        </resources>

    </build>



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接