【问题标题】:Thymeleaf : Getting value from a map while iterating through a listThymeleaf:在遍历列表时从地图中获取价值
【发布时间】:2016-12-20 23:14:11
【问题描述】:

我试图在遍历列表时从地图中获取值。 searchResult 包含Book 对象的列表,每个对象都有一个类别列表。 categoryFacetCounts 是一个包含每个类别值的计数的映射。该地图包含所有可能类别的值(我已调试和检查)。

以下是我的解决方案,它将null 打印为地图的输出:

<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>

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    使用地图的get方法,categoryFacetCounts

    <span th:each="c : ${book.categories}">
           <span th:text="${c} + '(' + ${categoryFacetCounts.get(c)} + ') '"></span>
    </span>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2022-08-17
      • 1970-01-01
      • 2014-03-02
      • 2022-01-13
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多