java - Thymeleaf 计数每个循环中的匹配项(对象数组)

标签 java thymeleaf

Counter = 0

<div th:each="student: ${students}">
    <div th:if="${student.name == 'Felix'}">
     Counter = Counter + 1
    <div/>
</div>

<div th:text="${Counter}"></div>

大家好,我仍然在基于数组中的匹配进行条件渲染方面遇到困难。当 Counter > 0 时是否有渲染某些内容的选项?我知道代码无效,但这只是所需的输出。 我知道 th:if 或类(class)。简单的 contains() 不起作用,因为数组包含多个对象,而不是字符串或易于使用的内容。

有人知道答案吗?

最佳答案

不,你不能这样做。您不应该更新 Thymeleaf 标记中的变量。要么:

  1. 在渲染之前添加计数(在模型上)。
  2. 使用collection selection .

    <div th:text="${#lists.size(students.?[name == 'Felix'])}" />
    

关于java - Thymeleaf 计数每个循环中的匹配项(对象数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590579/

相关文章:

java - 从列表中删除不相关的项目与使用相关项目构建新列表

html - 我可以更改 Thymeleaf "include"优先级吗?

javascript - 如何在具有 src 的 <img > 标签中附加 thymeleaf 变量?

java - 性能问题持续存在/使用 JPA 更新大量对象

java - 将 JLabel 置于另一个具有图像的 JLabel 之上

java - 构造函数 HashSet<Integer>(int[]) 未定义

java - 无法使用 spring 和 hibernate 5.0 执行空间查询

html - Thymeleaf:如何使用具有可变限制的#numbers.sequence()?

javascript - 第一次事件未在选项卡上发生

java - 如何向标准 thymeleaf 方言添加自定义属性 'th'