迭代 Thymeleaf 中的地图列表

Far*_*raz 2 java thymeleaf spring-boot

我有一个包含这样的地图的列表:

Map<String, Long> count = new HashMap<>();
count.put("totalMessageCount", 5L);         
Map<String, Map<String, Long>> names = new HashMap<>();
names.put("someKey", count);
List<Map<String, Map<String, Long>>> list = new ArrayList<>();
list.add(names);
Run Code Online (Sandbox Code Playgroud)

我将此列表从控制器发送到视图。

我已经尝试过这个:

<table>
        <tr th:each="element : ${list}">
        <td th:text="${element.key}"></td>
        <td th:text="${element.value}"></td>
</table>
Run Code Online (Sandbox Code Playgroud)

我收到错误:

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“java.util.HashMap”类型的对象上找不到属性或字段“key” - 可能不是公共的或无效?在 org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) 在 org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) 在 org.springframework.expression.spel .ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) 在...

任何帮助表示赞赏。

Lpp*_*Edd 5

您很可能需要另一层嵌套。

<table>
    <th:block th:each="map : ${list}">
       <tr th:each="e : ${map}">
          <td th:text="${e.key}"></td>
          <td th:text="${e.value}"></td>
       </tr>
    </th:block>
</table>
Run Code Online (Sandbox Code Playgroud)

Askeyvalue是 a 的属性Map.Entry