Thymeleaf 模板引擎入门案例 结合 SpringBoot框架(三)
- 使用thymeleaf 开发时,前端页面接收Controller的Collection数据,需要进行遍历:
- (一)针对form表单的遍历
th:each="emp:${emps}"
<!--每一个th:each都会生成一个tr标签-->
<!--遍历emps,取出的每个变量为emp-->
<tr th:each="emp:${emps}">
<td th:text="${emp.id}"></td>
<td>[[${emp.lastName}]]</td>
<td th:text="${emp.email}"></td>
<td th:text="${emp.gender}==0?'女':'男'"></td>
<td th:text="${emp.department.departmentName}"></td>
<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
<td>
<button class="btn btn-sm btn-primary">编辑</button>
<button class="btn btn-sm btn-danger">删除</button>
</td>
</tr>
- (二)针对下拉列表select的遍历
通过
th:text="${dept.departmentName}"显示下拉列表的属性值
th:value="${dept.id}" 获取下拉列表的真正值,
<select class="form-control">
<!--每一个th:each都会生成一个option标签-->
<option th:value="${dept.id}" th:each="dept:${departments}" th:text="${dept.departmentName}">1</option>
</select>
- 页面效果: