Thymeleaf的循环遍历方式有哪些

发布时间:2022-09-23 14:36:12 作者:iii
来源:亿速云 阅读:201

Thymeleaf的循环遍历方式有哪些

Thymeleaf 是一个流行的 Java 模板引擎,广泛用于服务器端渲染 HTML 页面。它提供了多种方式来遍历集合、数组或 Map 等数据结构。本文将介绍 Thymeleaf 中常用的循环遍历方式。

1. 使用 th:each 遍历集合

th:each 是 Thymeleaf 中最常用的循环遍历方式,适用于遍历集合、数组或 Map。它的基本语法如下:

<ul>
    <li th:each="item : ${items}" th:text="${item}"></li>
</ul>

在这个例子中,items 是一个集合或数组,item 是集合中的每个元素。th:each 会遍历 items 中的每个元素,并将其赋值给 item,然后在 li 标签中显示。

1.1 遍历 List

<ul>
    <li th:each="name : ${names}" th:text="${name}"></li>
</ul>

假设 names 是一个 List<String>,Thymeleaf 会遍历 names 列表中的每个元素,并将其显示在 li 标签中。

1.2 遍历 Map

<ul>
    <li th:each="entry : ${map}" th:text="${entry.key} + ': ' + ${entry.value}"></li>
</ul>

在这个例子中,map 是一个 Map<String, String>entry 是 Map 中的每个键值对。entry.keyentry.value 分别表示键和值。

2. 使用 th:each 遍历数组

th:each 也可以用于遍历数组。语法与遍历集合类似:

<ul>
    <li th:each="num : ${numbers}" th:text="${num}"></li>
</ul>

假设 numbers 是一个 int[] 数组,Thymeleaf 会遍历数组中的每个元素,并将其显示在 li 标签中。

3. 使用 th:each 遍历带有索引的集合

有时我们需要在遍历集合时获取当前元素的索引。Thymeleaf 提供了 th:each 的扩展语法来实现这一点:

<ul>
    <li th:each="name, iterStat : ${names}" th:text="${iterStat.index} + ': ' + ${name}"></li>
</ul>

在这个例子中,iterStat 是一个迭代状态对象,它包含了当前迭代的索引、计数等信息。iterStat.index 表示当前元素的索引(从 0 开始)。

3.1 迭代状态对象的属性

iterStat 对象提供了以下属性:

4. 使用 th:each 遍历嵌套集合

Thymeleaf 支持嵌套的 th:each 遍历,适用于处理嵌套的集合或 Map:

<ul th:each="category : ${categories}">
    <li th:text="${category.name}">
        <ul>
            <li th:each="product : ${category.products}" th:text="${product.name}"></li>
        </ul>
    </li>
</ul>

在这个例子中,categories 是一个包含多个 category 对象的集合,每个 category 对象又包含一个 products 集合。Thymeleaf 会先遍历 categories,然后在每个 category 中遍历 products

5. 使用 th:each 遍历带有条件的集合

Thymeleaf 允许在 th:each 中使用条件表达式来过滤集合中的元素:

<ul>
    <li th:each="name : ${names}" th:if="${name.startsWith('A')}" th:text="${name}"></li>
</ul>

在这个例子中,th:if 表达式用于过滤出以字母 “A” 开头的名字。

6. 使用 th:each 遍历带有默认值的集合

如果集合为空,Thymeleaf 提供了 th:unlessth:if 来处理默认值:

<ul>
    <li th:each="name : ${names}" th:text="${name}"></li>
    <li th:unless="${not #lists.isEmpty(names)}">No names found</li>
</ul>

在这个例子中,如果 names 集合为空,Thymeleaf 会显示 “No names found”。

7. 使用 th:each 遍历带有分页的集合

Thymeleaf 支持分页遍历集合,适用于处理大量数据:

<ul>
    <li th:each="name, iterStat : ${names}" th:if="${iterStat.index < 10}" th:text="${name}"></li>
</ul>

在这个例子中,th:if 表达式用于限制只显示前 10 个元素。

8. 使用 th:each 遍历带有排序的集合

Thymeleaf 允许在 th:each 中使用 #lists.sort 来对集合进行排序:

<ul>
    <li th:each="name : ${#lists.sort(names)}" th:text="${name}"></li>
</ul>

在这个例子中,#lists.sort(names) 会对 names 集合进行排序,然后遍历显示。

9. 使用 th:each 遍历带有自定义排序的集合

Thymeleaf 还支持自定义排序规则:

<ul>
    <li th:each="name : ${#lists.sort(names, T(java.util.Collections).reverseOrder())}" th:text="${name}"></li>
</ul>

在这个例子中,T(java.util.Collections).reverseOrder() 用于对 names 集合进行降序排序。

10. 使用 th:each 遍历带有分组的集合

Thymeleaf 支持对集合进行分组遍历:

<ul>
    <li th:each="group : ${#lists.group(names, 5)}">
        <ul>
            <li th:each="name : ${group}" th:text="${name}"></li>
        </ul>
    </li>
</ul>

在这个例子中,#lists.group(names, 5)names 集合分成每组 5 个元素,然后遍历显示。

总结

Thymeleaf 提供了多种灵活的循环遍历方式,适用于不同的场景。无论是简单的集合遍历,还是复杂的嵌套、分页、排序和分组,Thymeleaf 都能轻松应对。掌握这些遍历方式,可以帮助开发者更高效地处理数据展示逻辑。

推荐阅读:
  1. 对python For 循环的三种遍历方式解析
  2. JavaScript 中实现循环遍历的方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

thymeleaf

上一篇:Vue的双端diff算法怎么实现

下一篇:Pycharm报错Non-zero exit code (2)如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》