一、SpringBoot整合Thymeleaf
1.加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.3</version>
</dependency>
2.创建templates目录,在templates中创建html文件
3.在html中引入thymeleaf的命名空间
<html xmlns:th="http://www.thymeleaf.org">
4.创建controller提供一个访问的方法
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("mgs","hello thymeleaf");
return "hello";
}
}
5.在thymeleaf模板中取值
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${mgs}"></div>
</body>
</html>
二、Thymeleaf国际化
使用#读取配置文件中的内容,一般用于Thymeleaf国际化
三、变量获取语法
1.controller
@RequestMapping("/hello1")
public String hello1(Model model){
User user =new User("胖胖",18);
user.setRole(new Role("程序员"));
model.addAttribute("user",user);
return "hello";
}
2.在thymeleaf模板取值
<p th:text="${'姓名:'+user.name +' 年龄:'+user.age + ' 职业:'+user.role.roleName}"></p>
四、Thymeleaf变量中实用的对象
1.日期Dates
model.addAttribute("now",new Date());
<p th:text="${#dates.format(now,'yyyy-MM-dd HH:mm:ss')}"></p>
2.数字
model.addAttribute("number",0.02524);
<p th:text="${#numbers.formatPercent(number,2,2)}"></p>
3.字符串Strings
1)以某种字符拼接成字符串
model.addAttribute("strArray",new String[]{"aa","bb","cc"});
<p th:text="${#strings.arrayJoin(strArray,'-')} "></p
2)截取字符串
model.addAttribute("str","hellospringboot");
<p th:text="${#strings.substring(str,3,5)} "></p>
3)拆分字符串
model.addAttribute("strs","aa-bb-cc");
<p th:text="${#strings.listSplit(strs,'-')} "></p>
五、Thymeleaf变量中selections
@RequestMapping("/hello1")
public String hello1(HttpSession session){
User user =new User("胖胖",18);
user.setRole(new Role("程序员"));
session.setAttribute("user",user);
return "hello";
}
第一种形式
<div th:object="${session.user}">
<p>姓名: <span th:text="*{name}">Sebastian</span>.</p>
<p>年龄: <span th:text="*{age}">Pepper</span>.</p>
<p>职业: <span th:text="*{role.roleName}">Saturn</span>.</p>
</div>
第二种形式
<div>
<p>姓名: <span th:text="${session.user.name}">Sebastian</span>.</p>
<p>年龄: <span th:text="${session.user.age}">Pepper</span>.</p>
<p>职业: <span th:text="${session.user.role.roleName}">Saturn</span>.</p>
</div>
六、URL地址解析语法
controller
@RequestMapping("/myUrl")
public String myUrl(){
return "myUrl";
}
thymeleaf模板
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${3})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${3})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${3})}">view</a>
</body>
配置文件指定contextPath
server:
servlet:
context-path: /gtvg
七、Thymeleaf基本语法
<!--字符串-->
<span th:text="'The name of the user is ' + ${user.name}"/><br>
<span th:text="|Welcome to our application, ${user.name}!|"/><br>
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"/><br>
<span th:text="${user.name} + ' ' + |${user.age}, ${user.role.roleName}|"/>
<hr>
<!--数字-->
<div th:text="${user.age} % 2 == 0"/>
<div th:text="${user.age % 2 == 0} "/>
<hr>
<!--比较 age是否大于1-->
<div th:if="${user.age} > 1">
<span th:text="'Execution mode is ' + ( (${user.name} == '胖胖')? 'yes' : 'no')"/>
</div>
八、迭代Ineration
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<!--iterStat 状态变量-->
<!--even/odd 当前循序是否是偶数/奇数 -->
<tr th:each="users,iterStat : ${users}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${iterStat.index}"></td>
<td th:text="${users.name}"></td>
<td th:text="${users.age}"></td>
</tr>
九、Condition中的if
<tr th:each="users: ${users}">
<td th:text="${users.name}"></td>
<td th:text="${users.age}"></td>
<td>
<span th:text="${users.role==null?'-':users.role.roleName}"></span>
<a th:href="@{/user/roleDetail}"
th:if="${users.role!=null}">view</a>
</td>
</tr>