javaspringspring-data-jpathymeleafspring-el

Thymeleaf iterating a Java 8 Stream from Spring Data JPA


My Google-Fu is failing me so I ask you... Is there a way to iterate a Java 8 Stream with Thymeleaf similar the way one iterates a List while still keeping Stream's performance purpose?

Repository

Stream<User> findAll()

Model

Stream<User> users = userRepository.findAll();
model.addAttribute("users", users);

View

<div th:each="u: ${users}">
   <div th:text="${u.name}">

If I try this, I get:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'name' cannot be found on object of type 'java.util.stream.ReferencePipeline$Head' - maybe not public?

If I use a List instead, it works as expected.

Is there a proper way to handle a Stream that I'm failing to find?


Solution

  • As far as I know and looking on the Thymeleaf documentation there is no way to do what you want.

    Besides Thymeleaf doesn't provide any way to interact with streams, take into account that Stream objects hasn't access to its contained objects till you do a terminal operation (for example Collectors.toList())