java - thymeleaf : Getting value from a map while iterating through a list

标签 java spring-mvc thymeleaf

我试图在迭代列表时从 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/

相关文章:

spring - Spring Boot 中不存在所需的请求部分 'file'

java - 从单个服务多次调用多个微服务的最佳方法或设计模式是什么?

spring - 如何使用 Spring Boot Thymeleaf 显示当前登录的用户?

spring-mvc - Spring Boot + Thymeleaf : Bind empty form input to NULL-string

java - 检查 JMockit NonStrictExpectations 中的 "any"参数

java - 如何在ActionListener中调用另一个Java文件?

spring - Google 应用引擎与 Spring 框架的兼容性

java - 为什么我在 tomcat 中运行的 servlet 中启动类实例时出错?

Java,将null分配给对象和仅声明有什么区别

Thymeleaf onclick 将字符串作为参数值发送给 javascript 函数