How to loop through Map in Thymeleaf

后端 未结 2 1634
一个人的身影
一个人的身影 2021-02-01 01:00

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

相关标签:
2条回答
  • 2021-02-01 01:17

    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.

    0 讨论(0)
  • 2021-02-01 01:30

    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>
    
    0 讨论(0)
提交回复
热议问题