如何在 thymeleaf th:each 中使用另一个属性进行比较来过滤集合

2023-11-27

我正在尝试按照以下 url 中的示例使用 Thymeleaf 过滤集合。 “馆藏投影与选择”部分。http://doanduyhai.wordpress.com/2012/04/14/spring-mvc-part-iv-thymeleaf-advanced-usage/

<tr th:each="artist,rowStat : ${listArtits.?[alive == true]}">
...
</tr>

但是我想使用另一个属性而不是固定值(真/假)。例如

<tr th:each="artist,rowStat : ${listArtits.?[played > playedCountReq]}">
...
</tr>

其中 PlayCountReq 是 Thymeleaf 可用的另一个表单变量。我收到以下错误。在类型的对象上找不到属性或字段“playedCountReq”...

我尝试了多种方法但没有成功。有什么建议么?


我成功了:) 这是解决方案:

在控制器中:

(...)
Person p1 = new Person();
p1.setAge(20);
Person p2 = new Person();
p2.setAge(30);
List<Person> list = Lists.newArrayList(p1,p2);
modelMap.addAttribute("list", list);
Integer minAge = 13;
modelMap.addAttribute("minAge", minAge);
(...)

in html:

<table th:with="min=${minAge}">
<tr th:each="person,rowStat : ${list.?[age > __${min}__]}">
<td><span th:text="${person.age}"></span></td>
</tr>
</table>

Output:

30

希望这有帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 thymeleaf th:each 中使用另一个属性进行比较来过滤集合 的相关文章

随机推荐