睁眼写BUG,闭眼改BUG。

Spring Boot (2) 模板引擎

2019.06.24

前后端分离那么火热的情况, 模板引擎不能丢, 做嵌入式的后台还是很好的.

Spring Boot 整合视图层技术

详细参考旧址

Thymeleaf

创建Spring boot 项目, 添加 webthymeleaf 依赖.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Thymeleaf 配置

Spring boot 为Thymeleaf 提供了自动化配置类ThymeleafAutoCOnfiguration, 存在默认配置.

注意: html中 在html标签加上xmlns:th="http://www.thymeleaf.org"

常见配置:

# 是否开启缓存, 默认为true
spring.thymeleaf.cache=true
# 检查模板是否存在, 默认为true
spring.thymeleaf.check-template=true
# 检查模板位置是否存在, 默认为true
spring.thymeleaf.check-template-location=true
# 模板文件编码
spring.thymeleaf.encoding=UTF-8
# 模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
# Content-Type 配置
spring.thymeleaf.servlet.content-type=text/html
# 模板文件后缀
spring.thymeleaf.suffix=.html

参考自动配置类, 在propertiesyml 中配置Thymeleaf

FreeMarker

非常古老的模板引擎, 创建项目、添加依赖

参考官网