导入模板
1.要想使用thymeleaf模板,首先 导入thymeleaf启动器,点击官网查看依赖
2.导入依赖即可
thymeleaf赋值与遍历
Controller
@Controller
public class indexController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("user", "hello,spring");
List<String> list = new ArrayList<>();
list.add("hahha");
list.add("bababa");
model.addAttribute("users",list);
return "index";
}
}
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${user}"></div>
<div th:each="in:${users}" th:text="${in}"></div>
</body>
</html>