JAVA全系列 教程
3762个小节阅读:7088.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
语法 | 作用 |
---|---|
th:each | 迭代器,用于循环迭代集合 |
编写实体类
xxxxxxxxxx
public class User {
private String id;
private String name;
private int age;
// 省略getter/setter/构造方法
}
准备数据
xxxxxxxxxx
List<User> users = new ArrayList();
users.add(new User("1","sxt",23));
users.add(new User("2","baizhan",22));
users.add(new User("3","admin",25));
model.addAttribute("users",users);
在页面中展示数据
xxxxxxxxxx
<table border="1" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<!-- 遍历集合的每一项起名为user -->
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
准备数据
xxxxxxxxxx
Map<String,User> map = new HashMap();
map.put("user1",new User("1","sxt",23));
map.put("user2",new User("2","baizhan",22));
map.put("user3",new User("3","admin",25));
model.addAttribute("map",map);
遍历map
xxxxxxxxxx
<table border="1" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Key</th>
</tr>
<!-- 遍历出的是一个键值对对象,key获取键,value获取值 -->
<tr th:each="m : ${map}">
<td th:text="${m.value.id}"></td>
<td th:text="${m.value.name}"></td>
<td th:text="${m.value.age}"></td>
<td th:text="${m.key}"></td>
</tr>
</table>