我试图在迭代列表时从 map 中获取值。 searchResult
包含一个 Book
对象列表,每个对象都有一个类别列表。
categoryFacetCounts
是一个包含每个类别值的计数的映射。该 map 具有所有可能类别的值(我调试并检查过)。
下面是我的解决方案,它将 null
打印为 map 的输出:
<table class="table table-striped" id="watchlist-table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Categories</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${searchResult}">
<td th:text="${book.name}"></td>
<td th:text="${book.description}"></td>
<td>
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts[__${c}__]} + ') '"></span>
</span>
</td>
</tr>
</tbody>
</table>
最佳答案
使用 map 的get
方法,categoryFacetCounts
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts.get(c)} + ') '"></span>
</span>
希望这有帮助。
关于java - thymeleaf : Getting value from a map while iterating through a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943381/