Say if I have an ArrayList with user objects into it.
User.java
class User{
private Long id;
private String name;
}
I have a list with users
list.add(new User(1,"John");
list.add(new User(2,"Sam");
I want to check in thymeleaf if user List is having a user with name "Sam".
${#lists.contains(userList.name,'Sam')}
But the above doesn't work and throws Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression
I've also tried with " (doube-quotes)
You can achieve this using the Collection Selection feature of Spring Expression Language.
10.5.17 Collection Selection
Selection is a powerful expression language feature that allows you to transform some source collection into another by selecting from its entries.
Selection uses the syntax
?[selectionExpression]
. This will filter the collection and return a new collection containing a subset of the original elements.
In your particular case it would be:
${not userList.?[name == 'Sam'].isEmpty()}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With