spring - 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:
- The syntax of the selection (filtering) operator is :
${collection.?[property == value]}
- The syntax of the projection (mapping) operator is :
${collection.![property]}
This is fine if I have a list of objects, for example a list of persons. Then I can perform things like that:
- Selection (filtering): e.g.,
${persons.?[age >= 18]}
selects all persons of at least 18 years - Projection (mapping): e.g.,
${persons.![name]}
selects the name of every person
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.
解决方案
经过一番搜索,我在Spring Expression Language文档中找到了答案。
在10.5.11 变量中,文档说明了#this
和#root
变量。
变量#this 总是被定义并引用当前的评估对象(针对哪些不合格的引用被解析)。
因此,假设我有一个numbers
充满整数的列表,${numbers.?[#this >= 10]}
创建一个包含所有至少为 10 的数字的新列表。
推荐阅读
- linux - 如何在 shell scrit 中按字母顺序对 csv 文件进行排序?
- wifi - 15-30 分钟后失去 MQTT 连接
- go - 运行 time.Sleep inside a time.Tick 不遵守睡眠期
- kotlin - 如何使用 Kotlinx 序列化对类型化类进行编码?
- prolog - 如何在不使用追加的情况下在 Prolog 中连接 3 个列表
- reactjs - react simanntic ui,正在处理条件组件
- c# - C# ASP.NET 读取正文的第一个字节而不缓冲整个正文
- kubernetes - Helm v2 到 v3 迁移
- javascript - 如何在表单中使用复选框并控制输入字段
- javascript - 是否可以在 Playwright 中创建自定义命令?