官方文档:https://www.thymeleaf.org/documentation.html
1、controller
@Controller
public class IndexController {
@RequestMapping("/test")
public String index(Model model){
model.addAttribute("msg","<h1>hello springboot</h1>");
model.addAttribute("users", Arrays.asList("张三","李四"));
return "test";
}
}
2、html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<!--所有的html元素都可以被thymeleaf接管:th:元素名-->
<body>
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
<hr>
<h3 th:each="user:${users}" th:text="${user}"></h3>
<hr>
<h3 th:each="user:${users}">[[ ${user} ]]</h3>
</body>
</html>
结果