一.th:eath迭代集合用法:
<table> <thead> <tr> <th>序號</th> <th>用戶名</th> <th>密碼</th> <th>用戶昵稱</th> </tr> <tr th:each="user:${userlist}"> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.password}"></td> <td th:text="${user.petname}"></td> </tr> </thead> </table>
二.迭代下標變量用法:
狀態變量定義在一個th:每個屬性和包含以下數據:
1.當前迭代索引,從0開始。這是索引屬性。index
2.當前迭代索引,從1開始。這是統計屬性。count
3.元素的總量迭代變量。這是大小屬性。 size
4.iter變量為每個迭代。這是目前的財產。 current
5.是否當前迭代是奇數還是偶數。這些even/odd的布爾屬性。
6.是否第一個當前迭代。這是first布爾屬性。
7.是否最后一個當前迭代。這是last布爾屬性。
用法實例:
<table> <thead> <tr> <th>序號</th> <th>用戶名</th> <th>密碼</th> <th>用戶昵稱</th> </tr> <tr th:each="user,userStat:${userlist}" th:class="${userStat.odd}?'odd':'even'"> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.password}"></td> <td th:text="${user.petname}"></td> </tr> </thead> </table>
打印出status的值為:
如果你不顯式地設置迭代變量,Thymeleaf總是會為您創建一個統計的名字作為后綴iter變量:
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> </tr> <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> </tr> </table>