【问题标题】:Spring Boot + FreeMarker + RestController loading templatesSpring Boot + FreeMarker + RestController 加载模板
【发布时间】:2016-05-13 00:56:23
【问题描述】:

我使用 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 根本不会影响配置。

【问题讨论】:

标签: java spring spring-boot freemarker


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 2015-12-30
    • 2013-03-18
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2011-05-11
    • 2016-05-01
    相关资源
    最近更新 更多