# Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体
在controller中取出emps 对象数组
//1.查询所有的员工,返回列表页面
@GetMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();
//放在model请求域中
model.addAttribute("emps",employees);
//thymeleaf 默认就会拼串 //public static final String DEFAULT_PREFIX = "classpath:/templates/xxx.html";
//public static final String DEFAULT_SUFFIX = ".html";
return "emp/list";
}
在list.html中进行遍历
<tbody>
<tr th:each="emp:${emps}">
<td th:text="${emp.id}"></td>
<td th:text="${emp.lastName}"></td>
<td th:text="${emp.email}"></td>
<td th:text="${emp.gender}==1?'男':'女'"></td>
<td th:text="${emp.department.departmentName}"></td>
<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd')}"></td>
<td>
<button class="btn btn-sm btn-primary">编辑</button>
<button class="btn btn-sm btn-danger">删除</button>
</td>
</tr>
</tbody>
结果展示
你不逼自己一把,你永远都不知道自己有多优秀!只有经历了一些事,你才会懂得好好珍惜眼前的时光!