Search code examples
springspring-bootthymeleaf

How to access a list property of an object using thymleaf


How can I access a list variable contained inside the object in HTML using Thymleaf

Eg :All the aClient properties are String objects.They can be accessed as

<tr th:each="aClient : ${clientList}">
 <td th:text="${aClient.firstName}"/>
 <td th:text="${aClient.lastName}"/>
 <td th:text="${aClient.address}"/>
 <td th:text="${aClient.clientType}"/>

But for property like

List<Service> services.

How can I access it?

Tried doing below after some reading. But it did not pick up the names of services.

<div th:each="service, serv : *{services}">
<input type="text" th:field="*{service[__${serv.index}__].name}" />
</div>

"name" is the property in Service class with getters and setters.


Solution

  • Needed to insert the serivce names in the column.This is achieved by

    <td>
        <div th:each="service : ${aClient.services}" th:text="${service.name}"></div> 
    </td>