前后端分离那么火热的情况, 模板引擎不能丢, 做嵌入式的后台还是很好的.
Spring Boot 整合视图层技术
详细参考旧址
Thymeleaf
创建
Spring boot
项目, 添加 web、thymeleaf 依赖.
<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
参考自动配置类, 在
properties
或yml
中配置Thymeleaf
FreeMarker
非常古老的模板引擎, 创建项目、添加依赖