Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping list of objects to list of strings in thymeleaf

Tags:

thymeleaf

I have a list of User objects, and I want to transform it to a list of names, join it, and present it (not in a table). How do I do it?

class User {
  String name;
  String address;
}

I have the usersList, and I want to transform it to the list of names, something like this:

<body>
    <h1>Names of Users</h1>
    <span>
       <div th:text="${#strings.listJoin(usersList.map( u -> u.name).collect(Collectors.toList()), ',')}"></div>
    </span>
</body>

how can I do it in thymeleaf?

like image 796
Yossale Avatar asked Oct 26 '25 10:10

Yossale


1 Answers

You can do this with collection projection:

<body>
    <h1>Names of Users</h1>
    <span>
       <div th:text="${#strings.listJoin(usersList.![name], ',')}" />
    </span>
</body>
like image 54
Metroids Avatar answered Oct 29 '25 07:10

Metroids



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!