使用Thymeleaf循环列表时,有时候需要在前端显示出序号,
这时可以使用thymeleaf 循环的状态变量。
<tr th:each="service:${services}" >
<td th:text="${serviceStat.index+1}"></td>
<td th:text="${service.name}"></td>
<td th:text="${service.address}"></td>
<td th:text="${service.description}"></td>
</tr>
serviceStat是状态变量,有 index,count,size,current,even,odd,first,last等属性,如果没有显示设置状态变量,thymeleaf会默 认给个“变量名+Stat"的状态变量。
iterStat称作状态变量,属性有:
index:当前迭代对象的index(从0开始计算)
count: 当前迭代对象的index(从1开始计算)
size:被迭代对象的大小
current:当前迭代变量
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
first:布尔值,当前循环是否是第一个
last:布尔值,当前循环是否是最后一个