Issue
I have a List that contains a map like this:
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);
I am sending this list from controller to the View.
I have tried this:
<table>
<tr th:each="element : ${list}">
<td th:text="${element.key}"></td>
<td th:text="${element.value}"></td>
</table>
I get an error:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'key' cannot be found on object of type 'java.util.HashMap' - maybe not public or not valid? at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) at ...
Any help is appreciated.
Solution
You need another level of nesting, most probably.
<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>
As key
and value
are properties of a Map.Entry
.
Answered By - LppEdd
Answer Checked By - Gilberto Lyons (JavaFixing Admin)