1.引入依赖
<!--引入模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<!-- 不需要加版本号,因为有版本仲裁中心-->
</dependency>
控制端中传入数据:
@RequestMapping("/success")
public String success(Map<String, Object> map) {
System.out.println("进入success");
map.put("name", "熊哥");
map.put("age", "<h3>19</h3>");
map.put("users", Arrays.asList("a", "b", "c"));
return "success";
}
在templates文件夹中写html
success.html
<!DOCTYPE html>
<!-- 引入依赖链接,可以有提示-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>这是成功页面</h1>
<hr/>
<!--th:text会将所有标签中的文本内容替换-->
<div th:text="${name}">这是预定内容,将会被th:text给替代</div><hr/>
<!--th:id th:class同理-->
<div class="将会被替代的class" id="将会被替代的id" th:id="${name}" th:class="${name}"></div><hr/>
<h3 th:text="${age}">文本出现标签</h3>
<!--使用utext,内容不会出现<h3></h3> -->
<h3 th:utext="${age}">文本不出现标签</h3><hr/>
<!--遍历时,会创建列表个数个该标签-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3>
<span th:each="user:${users}" th:text="${user}"></span>
</h3>
<!--也可以直接写入文本-->
<h4 th:text="我做了改变"></h4>
</body>
</html>