thymeleaf中的th:each用法

一.th:eath迭代集合用法:spa

<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>

二.迭代下标变量用法:code

  状态变量定义在一个th:每一个属性和包含如下数据:blog

  1.当前迭代索引,从0开始。这是索引属性。index索引

  2.当前迭代索引,从1开始。这是统计属性。countit

  3.元素的总量迭代变量。这是大小属性。 size io

  4.iter变量为每一个迭代。这是目前的财产。 current table

  5.是否当前迭代是奇数仍是偶数。这些even/odd的布尔属性。  ast

  6.是否第一个当前迭代。这是first布尔属性。  class

  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>
相关文章
相关标签/搜索