thymeleaf 的 th:each 标签示例

简介: thymeleaf 的 th:each 标签示例

控制层:


@Controller
public class ThirdThymeleafController {
    @GetMapping("/third")
    public String third(Model model) {
        String msg = "hello,thymeleaf";
        User user1 = new User(1, "张三", 23);
        User user2 = new User(2, "李四", 25);
        User user3 = new User(3, "王五", 27);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        model.addAttribute("msg", msg);
        model.addAttribute("userList", list);
        return "index3";
    }
}


注:会将数据返回给 /src/main/resources/templates/index3.html 页面中,该路径为默认路径。


页面

<tr>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>index</td>
        <td>count</td>
        <td>size</td>
        <td>current</td>
        <td>first</td>
        <td>last</td>
    </tr>
    <tr th:each="user,iterStat:${userList}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.username}"></td>
        <td th:text="${user.age}"></td>
        <td th:text="${iterStat.index}">index</td>
        <td th:text="${iterStat.count}">count</td>
        <td th:text="${iterStat.size}">size</td>
        <td th:text="${iterStat.current}">current</td>
        <td th:text="${iterStat.first}">first</td>
        <td th:text="${iterStat.last}">last</td>
    </tr>


效果

20201109110219179.png


注:

iterStat 称作状态变量,属性有:

index:当前迭代对象的 index(从 0 开始计算)

count:当前迭代对象的 index(从 1 开始计算)

size:被迭代对象的大小

current:当前迭代变量

even/odd:布尔值,当前循环是否是偶数/奇数(从 0 开始计算)

first:布尔值,当前循环是否是第一个

last:布尔值,当前循环是否是最后一个


相关文章
|
11月前
thymeleaf th:href 多个参数传递格式
thymeleaf th:href 多个参数传递格式
|
6天前
|
XML 前端开发 PHP
ThinkPHP6 模板引擎普通标签中,模板引擎运算符函数,循环标签,判断标签的使用,及一些特殊标签
本文介绍了ThinkPHP6模板引擎中普通标签和XML标签的使用方法,包括模板引擎运算符函数、循环标签、判断标签以及一些特殊标签的使用。文中详细解释了普通标签的运算符和函数、注释、循环标签(foreach、volist、for)和判断标签(if、switch)的语法规范和示例。此外,还提到了literal和php标签用于原样输出和编写PHP代码的方法。
ThinkPHP6 模板引擎普通标签中,模板引擎运算符函数,循环标签,判断标签的使用,及一些特殊标签
|
11月前
|
Java
thymeleaf的each标签遍历取值
thymeleaf的each标签遍历取值
|
11月前
|
自然语言处理 前端开发 Java
SpringMVC表单标签
SpringMVC表单标签
|
11月前
Thymeleaf显示表格
Thymeleaf显示表格
123 0
|
前端开发 Unix Linux
【Thymeleaf】thymeleaf中给input的value属性设置值
【Thymeleaf】thymeleaf中给input的value属性设置值
521 0
|
XML SQL Java
JSTL 标签库,以及 out 和 set 标签|学习笔记
快速学习 JSTL 标签库,以及 out 和 set 标签
140 0
JSTL 标签库,以及 out 和 set 标签|学习笔记
|
索引
thymeleaf的th:each常见用法
thymeleaf的th:each常见用法 一.th:eath迭代集合用法: 是否选中 编号 姓名 年龄 编号 姓名 年龄 二.迭代下标变量用法: 状态变量定义在一个th:每个属性和包含以下数据: 1.当前迭代索引,从0开始。
4185 0