How to check if a list of objects contains a line with a timemeaf?

Tell me if I have an ArrayList with user objects.

User.java

class User{
  private Long id;
  private String name;
}

I have a list of users

list.add(new User(1,"John");
list.add(new User(2,"Sam");

I want to check thymeleaf if the List user has a user named "Sam".

${#lists.contains(userList.name,'Sam')}

But the above does not work and throws Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression

I also tried with "(Doube quotes)

+4
source share
2 answers

This can be done using the Spring Expression Language Collection Selection Function .

10.5.17 Collection Selection

Selection is a powerful expression language feature that allows you to convert some source collection to another by selecting its entries.

?[selectionExpression]. , .

:

${not userList.?[name == 'Sam'].isEmpty()}
+2
list.add(new User(1, "John");
list.add(new User(2, "Sam");

${list.contains(`Sam`)} or ${list.contains("Sam")}

-1

Source: https://habr.com/ru/post/1657490/


All Articles