Thymeleaf系列五 迭代,if,switch语法

x33g5p2x  于2021-12-24 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(433)

1. 概述

这里介绍thymeleaf的编程语法,本节主要包括如下内容

  1. 迭代语法:th:each; iteration status
  2. 条件语法:th:if; th:unless
  3. switch语法:th:switch; th:case; *

下文演示以上语法的用法。

2. 演示以上语法的用法

2.1. 公共类

User

  1. public class User {
  2. private String name;
  3. private boolean isAdmin;
  4. private String other;
  5. private int age;
  6. public User(String name, boolean isAdmin, String other, int age) {
  7. super();
  8. this.name = name;
  9. this.isAdmin = isAdmin;
  10. this.other = other;
  11. this.age = age;
  12. }
  13. // set/get略
  14. }

ProgrammingCtl : control类

  1. @Controller
  2. @RequestMapping("/programming")
  3. public class ProgrammingCtl {
  4. @RequestMapping("programming")
  5. public String iteration(ModelMap modeMap) {
  6. // Iteration
  7. List<User> userList = new ArrayList<User>();
  8. userList.add(new User("son_1", true, "other_1", 11));
  9. userList.add(new User("son_2", false, "other_2", 22));
  10. userList.add(new User("son_3", true, "other_3", 33));
  11. userList.add(new User("son_4", false, "other_4", 44));
  12. modeMap.put("userList", userList);
  13. // ifelse
  14. User userIf = new User("admin", true, "other_if", 11);
  15. modeMap.put("user", userIf);
  16. return "programming/programming";
  17. }
  18. }

本请求转到页面programming.html,

2.2. 迭代语法:th:each; iteration status

常用th:each用法:

  1. <table border="2">
  2. <thead>
  3. <tr>
  4. <th>name</th>
  5. <th>age</th>
  6. <th>isAdmin</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <!-- 常用的迭代 th:each 用法 -->
  11. <tr th:each="user : ${userList}">
  12. <td th:text="${user.name}"></td>
  13. <td th:text="${user.age}"></td>
  14. <td th:text="${user.isAdmin}"></td>
  15. </tr>
  16. </tbody>
  17. </table>

运行结果如下:

迭代的对象
本例子中迭代的对象是java.util.List,除了List,还可以对以下对象进行迭代

  • java.util.Iterable
  • java.util.Enumeration
  • java.util.Iterator
  • java.util.Map,此时迭代返回的对象是java.util.Map.Entry
  • 数组

获取迭代的中间的状态,定义在iterStat中

在迭代过程中,可以获取迭代的中间状态,详细如下:

  • index :当前节点的索引,从0开始
  • size : 迭代节点总数
  • even/odd:当前是偶数/奇数行,boolean值
  • first/last:当前是每天/最后一个元素
  1. <t able border="2">
  2. <thead>
  3. <tr>
  4. <th>迭代索引</th>
  5. <th>元素所处的位置索引</th>
  6. <th>奇偶行</th>
  7. <th>name</th>
  8. <th>age</th>
  9. <th>isAdmin</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <!-- 获取迭代的中间的状态,定义在iterStat中-->
  14. <tr th:each="user,iterStat : ${userList}">
  15. <!-- index: 当前迭代的索引 -->
  16. <td th:text="${iterStat.index }"></td>
  17. <!-- first: 当前元素是第一个元素; last: 当前元素是最后个元素 -->
  18. <td th:text="${iterStat.first } ? '这是第一个元素':(${iterStat.last} ? '这是最后一个元素':'')" ></td>
  19. <!-- -->
  20. <td th:text="${iterStat.odd} ? 'odd' : 'even'" ></td>
  21. <td th:text="${user.name}"></td>
  22. <td th:text="${user.age}"></td>
  23. <td th:text="${user.isAdmin}"></td>
  24. </tr>
  25. </tbody>
  26. </table>

运行结果如下:

2.3. 条件语法:th:if; th:unless

演示如下功能

  • th:if:如果值是true,则打印整个节点
  • th:unless: 和th:if是相反功能,如果值为false,则打印整个节点
  1. <!-- th:if:如果值是true,则打印<span>整个节点 -->
  2. <span th:if="${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />
  3. <!-- th:unless: 和th:if是相反功能,如果值为false,则打印<span>整个节点 -->
  4. <span th:unless="not ${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />

输出:

  1. <span>admin是管理员</span><br />
  2. <span>admin是管理员</span><br />

th:if条件判断
除了判断boolean值外,thymeleaf还认为如下表达式为true:

  • 值非空
  • 值是character,但是非0
  • 值是非0数字
  • 值是字符串,但是不是 “false”, “off” or “no”
  • 值不是boolean值,数字,character 或 字符串

2.4. switch语法:th:switch; th:case; *

演示如下功能

  • th:switch / th:case
  • th:case=”*” : 类似switch中的default
  1. <!-- th:switch / th:case -->
  2. <div th:switch="${user.name}">
  3. <p th:case="'admin'">User is an administrator</p>
  4. <!-- *: case的默认的选项 -->
  5. <p th:case="*">User is some other thing</p>
  6. </div>

输出:

  1. <div>
  2. <p>User is an administrator</p>
  3. </div>

3. 代码

代码详细见Github

相关文章