4.SpringBoot整合thymeleaf之list和map

一整合list

  1. 创建Controller
package com.synda.Controller;
/**
 * thymeleaf对集合的操作
 */
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.synda.entity.User;

@Controller
public class ListController {
	@RequestMapping("/list")
	public String getListInfo(Model model) {
		List<User> list = new ArrayList<User>();
		list.add(new User("1", "张三", "20"));
		list.add(new User("2", "里斯", "23"));
		list.add(new User("3", "王五", "28"));
		model.addAttribute("list", list);
		return "listIndex";

	}
}

  1. 创建listIndex.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>id</th>
			<th>name</th>
			<th>age</th>
		</tr>
		<tr th:each="user:${list}">
			<td th:text="${user.id}"></td>
			<td th:text="${user.name}"></td>
			<td th:text="${user.age}"></td>
		</tr>
	</table>
	<table border="1">
		<tr>
			<th>index</th>
			<th>count</th>
			<th>even</th>
			<th>odd</th>
			<th>size</th>
			<th>first</th>
			<th>last</th>
		</tr>
		<!-- index为集合状态 -->
		<tr th:each="user,index:${list}">
		<!-- 当前迭代对象的索引,从零开始 -->
			<td th:text="${index.index}"></td>
			<!-- 当前迭代对象的数量 -->
			<td th:text="${index.count}"></td>
			<!-- 当前循环次数为基数还是偶数 -->
			<td th:text="${index.even}"></td>
			<td th:text="${index.odd}"></td>
			<!-- 迭代对象的数量 -->
			<td th:text="${index.size}"></td>
			<!-- 当前是否为第一次循环 -->
			<td th:text="${index.first}"></td>
			<!-- 当前是否为第二次循环 -->
			<td th:text="${index.last}"></td>
		</tr>
	</table>
</body>
</html>
  1. 执行结果
    在这里插入图片描述

二、整合map

  1. 创建Controller
package com.synda.Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.synda.entity.User;

@Controller
public class mapController {
	@RequestMapping("/map")
	public String getMapInfo( Model model) {
		Map<String, User> map = new HashMap<String, User>();
		map.put("u1", new User("1", "张三", "20"));
		map.put("u2", new User("2", "里斯", "21"));
		map.put("u3", new User("3", "王五", "22"));
		model.addAttribute("map", map);
		return "mapIndex";
	}
}

  1. 创建mapIndex.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr> 
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<tr th:each="maps:${map}">
<td th:each="entry:${maps}" th:text="${entry.value.id}"></td>
<td th:each="entry:${maps}" th:text="${entry.value.name}"></td>
<td th:each="entry:${maps}" th:text="${entry.value.age}"></td>
</tr>
</table>
</body>
</html>

3.执行结果
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

synda@hzy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值