Frage

I am trying to understand how to loop through all entries in a Map in Thymeleaf. I have a domain object being processed by Thymeleaf that contains a Map.

How do I loop through the keys and fetch the values ?

Thanks.

War es hilfreich?

Lösung

Nevermind... I found it...

<tr th:each="instance : ${analysis.instanceMap}">
    <td th:text="${instance.key}">keyvalue</td>
    <td th:text="${instance.value.numOfData}">num</td>
</tr>

Thanks.

Andere Tipps

In case you have a List as the value. For example, when you have a map with key being the category, and value being a list of items pertaining to that category, you can use this:

<table>
    <tr th:each="element : ${catsAndItems}">
        <td th:text="${element.key}">keyvalue</td>
        <table>
            <tr th:each="anews : ${element.value}">
                <td th:text="${anews.title}">Some name</td>
                <td th:text="${anews.description}">Some name</td>
                <td th:text="${anews.url}">Some name</td>
                <td th:text="${anews.logo}">Some name</td>
                <td th:text="${anews.collectionDate}">Some name</td>
            </tr>
        </table>
    </tr>
</table>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top