Spring Boot + FreeMarker + RestController加载模板

时间:2016-05-13 00:56:23

标签: java spring spring-boot freemarker

我使用FreeMarker作为构建字符串的方法,这些字符串是作为对REST请求的响应放在线上的POJO的属性。我已将Freemarker添加到项目的Maven pom中,我可以将配置注入到我的服务中,但我必须做一个特殊的事情才能让类加载器找到我的模板。

     private Template getTemplate() throws Exception {
        configuration.setClassForTemplateLoading(this.getClass(), "/template/");

        Template temp = configuration.getTemplate("test.ftl");

        return temp;
    }

是否有更简洁的方法来配置FreeMarker查找模板的位置?使用@Bean生成FreeMarkerConfigurer根本不会影响配置。

1 个答案:

答案 0 :(得分:0)

http://freemarker.org/docs/pgui_config_templateloading.html

您可以将ftl模板放在src / main / resources / templates文件夹中,它将自动识别为免费标记版本:2.3.26-在spring boot中孵化。否则,请在application.properties文件中使用以下代码,并自动装配使用的对象。

spring.freemarker.template-loader-path=relativefolder/

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html