如何在Thymeleaf中循环浏览地图

phi*_*e.b 43 spring-mvc thymeleaf

我试图了解如何遍历Thymeleaf中的Map中的所有条目.我有一个由Thymeleaf处理的域对象,其中包含一个Map.

如何循环键并获取值?

谢谢.

phi*_*e.b 98

没关系......我发现了......

<tr th:each="instance : ${analysis.instanceMap}">
    <td th:text="${instance.key}">keyvalue</td>
    <td th:text="${instance.value.numOfData}">num</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

谢谢.

  • [“迭代地图时,迭代变量将属于java.util.Map.Entry类。”](http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#iterable-values) (3认同)
  • 这件事的根源是什么? (2认同)

ACV*_*ACV 29

如果您有一个List作为值.例如,如果您有一个地图,其中key是类别,而value是与该类别相关的项目列表,则可以使用以下命令:

<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>
Run Code Online (Sandbox Code Playgroud)