Thymeleaf——th:each遍历Map List

@RequestMappingpublic Object a3{Map<String,User> map1=new HashedMap();User user1

@RequestMapping("/4")
	public Object a3(Model model){
		Map<String,User> map1=new HashedMap();
		User user1;
		for(int i=0;i<10;i++) {
			user1=new User();
			user1.setUsername("xiao.li"+i);
			user1.setAge(i);
			user1.setAddress("XXX的地址是"+i*2);
			map1.put("J"+i, user1);
		}
		model.addAttribute("userMap",map1);
		List<Map<String,User>> list = new ArrayList<Map<String,User>>();
		User user;
		for(int i=0;i<10;i++) {
			Map<String,User> map=new HashedMap();
				user=new User();
				user.setUsername("xiao.li"+i);
				user.setAge(i);
				user.setAddress("XXX的地址是"+i*2);
				map.put("A"+i, user);
			list.add(map);
		}
		model.addAttribute("userList",list);
		return "bb";
	}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
 	<table border="1" >
		<tr><td>No.</td>
			<td>userStat</td>
			<td>userEntry</td>
			<td>UserObject</td>
			<td>username</td>
			<td>age</td>
			<td>address</td>
		</tr>
		<tr th:each="userEntry,userStat:${userMap}">
			<td th:text="${userStat.index}"></td>
			<td th:text="${userStat}"></td>
			<td th:text="${userEntry}"></td>
			<td th:text="${userEntry.value}"></td>
			<td th:text="${userEntry.value.username}"></td>
			<td th:text="${userEntry.value.age}"></td>
			<td th:text="${userEntry.value.address}"></td>
		</tr>
	</table> 
	<br/>
	<table border="1" >
		<tr><td>No.</td>
			<td>userStat</td>
			<td>userEntry</td>
			<td>UserObject</td>
			<td>username</td>
			<td>age</td>
			<td>address</td>
		</tr>
		<tr th:each="userEntry,userStat:${userList}">
		<!-- 
			userStat称作状态变量,属性有:
			    index:当前迭代对象的index(从0开始计算)
			    count: 当前迭代对象的index(从1开始计算)
			    size:被迭代对象的大小
			    current:当前迭代变量   //userStat.current等同于userEntry
			    even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
			    first:布尔值,当前循环是否是第一个
			    last:布尔值,当前循环是否是最后一个
		 -->
			<td th:text="${userStat.index}"></td>
			<td th:text="${userStat}"></td>
			<td th:text="${userEntry}"></td>
			<td th:text="${userStat.current['A'+userStat.index]}"></td>
			<td th:text="${userStat.current['A'+userStat.index].username}"></td>
			<td th:text="${userStat.current['A'+userStat.index].age}"></td>
			<td th:text="${userStat.current['A'+userStat.index].address}"></td>
		</tr>
	</table>
</body>
</html>

发表评论

登录 后才能评论