using thymeleaf 之 th:each迭代循环

时间:2025-02-15 08:07:15

th:each属性用于迭代循环,语法:th:each="obj,iterStat:${objList}"

迭代对象可以是,,数组等;

iterStat称作状态变量,属性有:
    index:当前迭代对象的index(从0开始计算)
    count: 当前迭代对象的index(从1开始计算)
    size:被迭代对象的大小
    current:当前迭代变量
    even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
    first:布尔值,当前循环是否是第一个
    last:布尔值,当前循环是否是最后一个

<ol>
		<li>List循环:
			<table border="1">
		      <tr>
		        <th>用户名</th>
		        <th>邮箱</th>
		        <th>管理员</th>
		        <th>状态变量:index</th>
		        <th>状态变量:count</th>
		        <th>状态变量:size</th>
		        <th>状态变量:</th>
		        <th>状态变量:even</th>
		        <th>状态变量:odd</th>
		        <th>状态变量:first</th>
		        <th>状态变量:last</th>
		      </tr>
		      <tr  th:each="user,userStat : ${list}">
		        <td th:text="${}">Onions</td>
		        <td th:text="${}">test@</td>
		        <td th:text="${}">yes</td>
		         <th th:text="${}">状态变量:index</th>
		        <th th:text="${}">状态变量:count</th>
		        <th th:text="${}">状态变量:size</th>
		        <th th:text="${}">状态变量:current</th>
		        <th th:text="${}">状态变量:even****</th>
		        <th th:text="${}">状态变量:odd</th>
		        <th th:text="${}">状态变量:first</th>
		        <th th:text="${}">状态变量:last</th>
		      </tr>
	    	</table>
		</li>
		<li>Map循环:
			<div th:each="mapS:${map}">
			<div th:text="${mapS}"></div>
			</div>
		</li>
		<li>数组循环:
			<div th:each="arrayS:${arrays}">
			<div th:text="${arrayS}"></div>
			</div>
		</li>
		</ol>