<!--以下两个依赖是thymeleaf模板引擎使用所需要的引擎-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
取值的集中方法
变量 ${...}
消息 #{...}
URL @{...}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--所有的html元素都可以被thymeleaf替换接管: th:元素名 -->
<div th:text="${msg}"></div>
<p>${msg}</p> <!--取出controller中定义的msg变量,提取失败,因为方法不对-->
</body>
</html>
@Controller
@RequestMapping(value = "/hello")
public class helloController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String hello(Model model){
model.addAttribute("msg","<h1>hello test<h1>");//定义一个变量,然后在test.html中取出
model.addAttribute("user", Arrays.asList("aaa","bbb"));
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is test
<!--所有的html元素都可以被thymeleaf替换接管: th:元素名 -->
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
<p>${msg}</p> <!--取出controller中定义的msg变量,提取失败,因为方法不对-->
<hr>
<div th:each="user:${user}">[[ ${user} ]]</div>
<hr>
<div th:each="user:${user}" th:text="${user}"></div>
</body>
</html>