0

I have more than 300 controllers. I would like to have one and only one HTML page for all controllers.

I know to make loop over a list like this in thyeleaf.

<tbody>
    <tr th:each="client : ${customerList}">
        <td th:text="${client.getClientID()}"></td>
        <td th:text="${client.getIpAddress()}"></td>
        <td th:text="${client.getDescription()}"></td>
    </tr>
</tbody>

But how to iterate over any list without specifying properties like clientID or ipAddress or description? And be able to do something like this with generic operation:

<td><a th:href="'/delete/' + ${client.getClientID}">Delete</a></td>
<td><a th:href="'/edit/' + ${client.getClientID}">Edit</a></td>

hope to be clear

Please see picture.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Lastu
  • 9
  • 5

2 Answers2

0

You dont have to specify getClientID(), getIpAddress() at all for just looping through the list.

<tr th:each="client : ${customerList}">
    <td>Repeat me</td>
</tr>

If you use the above loop you will get <td>Repeat me</td> as many times as the size of the list customerList.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
0

You can store the data in your model as List<Map<String, String>> where every object of the list is represented as a map, map's key is object attribute name, and value is object's attribute value.

Then on your view, this will do:

<table th:if="${not #lists.isEmpty(listOfMaps)}">
   <thead>
      <!--Columns detection/display -->
      <th th:each="firstObjEntry: ${listOfMaps[0]}" th:text="${firstObjEntry['key']}">
      </th>
   </thead>
   <tbody>
      <tr th:each="objMap: ${listOfMaps}">
         <td th:each="objMapEntry : ${objMap}" th:text="${objMapEntry['value']}"></td>
      </tr>
   </tbody>
</table>
isah
  • 5,221
  • 3
  • 26
  • 36
  • seems very complicated Isah! have you a sample example plz? thanks – Lastu Aug 21 '17 at 13:14
  • The problem you're trying to solve is not simple, there's no simple way around it. All you have to do is convert your list of objects to list of maps, then this table above will display that list of maps. For converting java objects to Maps, see this https://stackoverflow.com/questions/739689/how-to-convert-a-java-object-bean-to-key-value-pairs-and-vice-versa – isah Aug 21 '17 at 13:49