首页 > 解决方案 > Filter integer list in Thymeleaf

问题描述

I am learning about Thymeleaf in Spring, and I am struggling with list fitlering.

The official Tutorial: Using Thymeleaf does not talk about collection filtering and projection, but I found out that Thymeleaf on Spring uses the Spring Expression Language.

This guide states the following:

This is fine if I have a list of objects, for example a list of persons. Then I can perform things like that:


Question:

What if I do not have a list of objects (such as a list of persons) but a list of numbers or list of Strings? How can I perform selection (filtering) then? Things like numbers.?[>10] does not work.

标签: springspring-bootthymeleafspring-expressionspring-expression-language

解决方案


经过一番搜索,我在Spring Expression Language文档中找到了答案。

10.5.11 变量中,文档说明了#this#root变量。

变量#this 总是被定义并引用当前的评估对象(针对哪些不合格的引用被解析)。

因此,假设我有一个numbers充满整数的列表,${numbers.?[#this >= 10]}创建一个包含所有至少为 10 的数字的新列表。


推荐阅读