【【SpringBoot】四、】四、SpringBoot中整合中整合Freemarker
FreeMarker 是一款模板引擎:是一款模板引擎: 即一种基于模板和要改变的数据,即一种基于模板和要改变的数据, 并用来生成输出文本(并用来生成输出文本( HTML 网页、电子邮件、配置文网页、电子邮件、配置文
件、源代码等)的通用工具。件、源代码等)的通用工具。
它不是面向最终用户的,而是一个 Java 类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker 是一个很值得去学习的模版引擎。它是基于模板文件生成其他文本的通用工具。
本文主要介绍在 SpringBoot 中如何整合 FreeMarker,一起来看看吧!!!
1、引入、引入 Freemarker 依赖依赖
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
通过 org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,我们可以看到 FreeMarker 的自动
化配置,在这个类的构造方法中,注入了 FreeMarkerProperties:
@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = ".ftl";
/**
* Well-known FreeMarker keys which are passed to FreeMarker's Configuration.
*/
private Map settings = new HashMap();
}
FreeMarkerProperties 中则配置了 Freemarker 的基本信息,例如模板位置在 classpath:/templates/ ,再例如模板后缀为 .ftl,
那么这些配置我们以后都可以在 application.yml 中进行修改。
2、在、在 application.yml 中进行配置中进行配置 Freemarker
当然,SpingBoot 已经帮我们自动配置好了关于 Freemarker 的一系列配置,但是我们仍可以对其进行个性化配置
spring:
freemarker:
# 是否开启缓存
cache: false
# 模板文件编码
charset: UTF-8
# 是否检查模板位置
check-template-location: true
# Content-Type的值
评论0
最新资源