Issue
I have built an application in Spring-MVC with Thymealeaf and I would like to iterate in a table in Thymealeaf the DB entries.
This is the object that I am iterating:
List<Map<String, Object>> map
I tried this code in Thymleaf:
<table>
<tr>
<td> CNP:</td>
<td th:text ="${raspuns.cif}" ></td>
</tr>
<tr>
<td> Nume:</td>
<td th:text ="${raspuns.den_client}" ></td>
</tr>
</table>
But it shows only 2 entries and I know it has 311 or something like that. How could I iterate through all the entries and show them all?
Solution
You have to iterate list first, and then read each map from list & then read map object with the help of key like this:
Java Code:
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, Object> firstMap = new HashMap<>();
firstMap.put("id", "1");
firstMap.put("name", "test_name_1");
Map<String, Object> secondMap = new HashMap<>();
secondMap.put("id", "2");
secondMap.put("name", "test_name_2");
mapList.add(firstMap);
mapList.add(secondMap);
model.put("map_list", mapList);
Thymeleaf Code:
<tr th:each="map : ${map_list}">
<td> CNP:</td>
<td th:text ="${map.get('id')}" ></td>
<td> Nume:</td>
<td th:text ="${map.get('name')}" ></td>
</tr>
Answered By - Abhale Amol
Answer Checked By - Marilyn (JavaFixing Volunteer)