简体   繁体   English

如何从 Thymeleaf 获取特定的 map 值?

[英]How can I get a specific map value from Thymeleaf?

How can I get a specific map value from Thymeleaf?如何从 Thymeleaf 获取特定的 map 值?

        <tr th:each="item : ${items}">
            <td th:each="element : ${item}">
                   <a th:text="${element['key']}">anchor</a>
            </td>
        </tr>

In the code above, all keys corresponding to an element are displayed.在上面的代码中,显示了与元素对应的所有键。

But what I want is to get element['projectName'] .但我想要的是获得element['projectName'] That is, I want it to be something like this:也就是说,我希望它是这样的:

        <tr th:each="item : ${items}">
            <td th:each="element : ${item}">
                   <a th:text="${element['key']}"
                        th:href="${element.get('projectName')}">anchor</a>

            </td>
        </tr>

The data type of items is List<Map<String, Object>> , and the values in items are items 的数据类型为List<Map<String, Object>> ,items 中的值为

        assertThat(innerItems.get(0).get("modelName")).isEqualTo("a");
        assertThat(innerItems.get(0).get("item")).isEqualTo(1);
        assertThat(innerItems.get(0).get("id")).isEqualTo("q");
        assertThat(innerItems.get(0).get("projectName")).isEqualTo("DefaultProject");
        assertThat(innerItems.get(0).get("user_poi_no")).isEqualTo(1);
is.

Looks like it would be better to loop by key in your inner loop if I get it right.如果我做对了,看起来在你的内部循环中按键循环会更好。

    <tr th:each="item : ${items}">
        <td th:each="key: ${item.keySet()}">
              <a th:text="${key}" th:href="${item.get(key)}">anchor</a>
       </td>
    </tr>

When you loop over a map, you get a special java.util.Map.Entry element with a key and a value , not the actual item itself.当您遍历 map 时,您会得到一个特殊的java.util.Map.Entry元素,其中包含一个key和一个value ,而不是实际项目本身。 In the example you gave, using item instead of element should work (if that is indeed what you want):在您给出的示例中,使用item而不是element应该可行(如果这确实是您想要的):

<tr th:each="item : ${items}">
  <td th:each="element : ${item}">
    <a th:text="${element['key']}" th:href="${item['projectName']}">anchor</a>
  </td>
</tr>

If you just wanted the projectName from each item in your list, you don't even need the inner loop.如果您只需要列表中每个项目的projectName ,则甚至不需要内部循环。 Like this for example:像这样例如:

<tr th:each="item : ${items}">
  <td>
    <a th:text="${item['projectName']}">anchor</a>
  </td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM