pom文件引入依赖:
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
yml配置:
spring:
#thymeleaf页面模板配置
mvc:
view:
prefix: /
suffix: .html
UserController:
@GetMapping("/user/list")
private String helloWorld(Model model) {
List<UserPojo> userPojoList = new ArrayList<>();
userPojoList.add(new UserPojo("1a2b3c", "测试用户1", "pwd111aaa"));
userPojoList.add(new UserPojo("4d5e6f", "测试用户2", "pwd222bbb"));
model.addAttribute("users", userPojoList);
return "list";
}
list.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul th:each=" user : ${users}">
<li th:text="${user.id}"></li>
</ul>
</body>
</html>
结果:
1a2b3c
测试用户1
pwd111aaa
4d5e6f
测试用户2
pwd222bbb
问题:
1.遍历时利用嵌套标签如:ul-li,tr-td,不要使用非嵌套标签如:p,防止遍历失败;