SpringBoot集成Thymeleaf总结
- 表达式语法:
- ${}:变量表达式,用法:
${session.user.name}
<span th:text="${book.author.name}">
< th:each="book : ${books}">
- *{}:选择表达式
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
//上述这段代码等同于
<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
- #{}消息表达式(i18n)
- @{}:链接表达式(url)
- ~{}:分段表达式
- 在开发中需要把Thymeleaf缓存关闭。用法(application.properties中添加spring.thymeleaf.cache = false)
- Spring Boot集成Thymeleaf步骤:
- 搭建spring boot开发环境
- 在src/main/resource下新建i18n和templates两个文件夹
- 在application.properties中加入如下配置
#在构建URL时预先查看名称的前缀
spring.thymeleaf.prefix = classpath:/templates/
#thymeleaf国际化配文件路径
spring.messages.basename=i18n/messages
#不缓存
spring.thymeleaf.cache=false
- 在templates文件夹下新建index.html文件
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>测试Thymeleaf</title>
</head>
<body>
<button th:text="#{sys.button.submit}">rr</button>
</body>
</html>