springboot整合thymeleaf
1. 导入起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 更改引入版本
<properties>
<springboot-thymeleaf.version>3.0.2.RELEASE</springboot-thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
- springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。
- 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。
- 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。
3. 配置文件配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false
语法
1. 替换标签体内容
<!--渲染效果-->
<div th:text="${Hello}">显示欢迎</div>
<!--不渲染效果-->
<div th:utext="${Hello}">显示欢迎</div>
2. 替换属性
<div id="div01" th:id="${Hell