后端Controller通过model可以对变量
、数组
、list集合
、map
以及对象
进行传值。
@RequestMapping("/test")
public String Index(Model model){
model.addAttribute("user","helloWorld");//变量
model.addAttribute("msg",Arrays.asList("build","classMethod"));//遍历数组
return "index";
}
前端html中需先引入Thymeleaf路径:xmlns:th="http://www.w3.org/1999/xhtml"
,后需在标签中加入th:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<div th:text="${user}"></div>
<p th:each="megs:${msg}" th:text="${megs}"></p>
</body>
</html>