一.依赖导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二.引入模板
模板路径:“classpath:/templates/xxx.html”
在模板中引入头文件约束:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
三.模板使用
1.文本取值
传值:
@RequestMapping("/test")
public String index(Model model){
model.addAttribute("msg","<h1>hhh</h1>");
return "test";
}
- text取值(不带转译)
<div th:text="${msg}"></div>
页面显示:

- utext取值(带转译)
<div th:utext="${msg}"></div>
页面显示:

2.数组遍历
@RequestMapping("/test")
public String index(Model model){
model.addAttribute("users", Arrays.asList("ming","mingiao"));
return "test";
}
遍历取值:
- 第一种:
<h3 th:each="user:${users}" th:text="${user}"></h3>
- 第二种:
<h3 th:each="user:${users}">[[${user}]]</h3>
结果显示:

版权声明:本文为mingiao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。