您好,登录后才能下订单哦!
Thymeleaf 是一个流行的 Java 模板引擎,广泛用于服务器端渲染 HTML 页面。它提供了多种方式来遍历集合、数组或 Map 等数据结构。本文将介绍 Thymeleaf 中常用的循环遍历方式。
th:each 遍历集合th:each 是 Thymeleaf 中最常用的循环遍历方式,适用于遍历集合、数组或 Map。它的基本语法如下:
<ul>
<li th:each="item : ${items}" th:text="${item}"></li>
</ul>
在这个例子中,items 是一个集合或数组,item 是集合中的每个元素。th:each 会遍历 items 中的每个元素,并将其赋值给 item,然后在 li 标签中显示。
<ul>
<li th:each="name : ${names}" th:text="${name}"></li>
</ul>
假设 names 是一个 List<String>,Thymeleaf 会遍历 names 列表中的每个元素,并将其显示在 li 标签中。
<ul>
<li th:each="entry : ${map}" th:text="${entry.key} + ': ' + ${entry.value}"></li>
</ul>
在这个例子中,map 是一个 Map<String, String>,entry 是 Map 中的每个键值对。entry.key 和 entry.value 分别表示键和值。
th:each 遍历数组th:each 也可以用于遍历数组。语法与遍历集合类似:
<ul>
<li th:each="num : ${numbers}" th:text="${num}"></li>
</ul>
假设 numbers 是一个 int[] 数组,Thymeleaf 会遍历数组中的每个元素,并将其显示在 li 标签中。
th:each 遍历带有索引的集合有时我们需要在遍历集合时获取当前元素的索引。Thymeleaf 提供了 th:each 的扩展语法来实现这一点:
<ul>
<li th:each="name, iterStat : ${names}" th:text="${iterStat.index} + ': ' + ${name}"></li>
</ul>
在这个例子中,iterStat 是一个迭代状态对象,它包含了当前迭代的索引、计数等信息。iterStat.index 表示当前元素的索引(从 0 开始)。
iterStat 对象提供了以下属性:
index: 当前迭代的索引(从 0 开始)。count: 当前迭代的计数(从 1 开始)。size: 集合的大小。even: 当前迭代是否为偶数行。odd: 当前迭代是否为奇数行。first: 当前迭代是否为第一个元素。last: 当前迭代是否为最后一个元素。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。
th:each 遍历带有条件的集合Thymeleaf 允许在 th:each 中使用条件表达式来过滤集合中的元素:
<ul>
<li th:each="name : ${names}" th:if="${name.startsWith('A')}" th:text="${name}"></li>
</ul>
在这个例子中,th:if 表达式用于过滤出以字母 “A” 开头的名字。
th:each 遍历带有默认值的集合如果集合为空,Thymeleaf 提供了 th:unless 和 th: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”。
th:each 遍历带有分页的集合Thymeleaf 支持分页遍历集合,适用于处理大量数据:
<ul>
<li th:each="name, iterStat : ${names}" th:if="${iterStat.index < 10}" th:text="${name}"></li>
</ul>
在这个例子中,th:if 表达式用于限制只显示前 10 个元素。
th:each 遍历带有排序的集合Thymeleaf 允许在 th:each 中使用 #lists.sort 来对集合进行排序:
<ul>
<li th:each="name : ${#lists.sort(names)}" th:text="${name}"></li>
</ul>
在这个例子中,#lists.sort(names) 会对 names 集合进行排序,然后遍历显示。
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 集合进行降序排序。
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 都能轻松应对。掌握这些遍历方式,可以帮助开发者更高效地处理数据展示逻辑。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。