Thymeleaf中"th:each""th:if"的用法解析

"th:each"用于迭代遍历

<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>
      </thead>
      <tbody>
                                
           <tr th:each="teacher,count:${teachers.list}">
               <td th:text="${count.count}"></td>
               <td th:text="${teacher.teacherName}"></td>
               <td th:text="${teacher.teacherSex}"></td>
               <td th:utext="${teacher.teacherNo}"></td>
            </tr>
     </tbody>
</table>

第一列是一个从1开始的序号列
在这里插入图片描述

"th:if"用于判断

用法:

th:if="${xx} lt 'x'"  <-----------> xx < x  

thymeleaf 判断表达式:

gt:great than(大于)>
ge:great equal(大于等于)>=
eq:equal(等于)==
lt:less than(小于)<
le:less equal(小于等于)<=
ne:not equal(不等于)!=

一个实例

<tr th:each="user:${users}">
     <td th:if="${user.num} eq "123" th:text="${user.username}">
     </td>
</tr>
发布了71 篇原创文章 · 获赞 14 · 访问量 5612

猜你喜欢

转载自blog.csdn.net/flying_hengfei/article/details/103077172
今日推荐