【问题标题】:How to find a specific element and get a field value from a list of objects in thymeleaf如何从 thymeleaf 中的对象列表中查找特定元素并获取字段值
【发布时间】:2021-02-18 05:00:22
【问题描述】:

我在 thymeleaf 中有这个块,我试图在其中显示 protocolVersion 的名称而不是 id。我只有 Id,所以我传递了 protocolVersion (protocolVersions) 列表,并对其进行迭代以显示与 id 匹配的那个(1007 只是一个测试)。

<th:block th:each="item: ${protocolVersions}">
  <tr th:if="${item.id == 1007}">
    <td th:text="${item.name}"></td>
  </tr>
</th:block>

我收到错误消息:

评估 SpringEL 表达式的异常:“item.id == 1007”

我也尝试过这样的事情,因为我在这里的一个问题中找到了它:

<td th:if="${protocolVersions.?[id == '${excelMongoDoc.protocolVersionId}']}"
  th:text="${protocolVersions[id == '${excelMongoDoc.protocolVersionId}'].name}">
</td>

但这也不起作用。有人可以帮忙吗?

谢谢

【问题讨论】:

    标签: html spring-mvc thymeleaf


    【解决方案1】:

    表达式很简单:${protocolVersions.^[id==1007].name}。所以你可以在你的表中这样做:

    <tr>
      <td th:text="${protocolVersions.^[id==1007].name}"></td>
    </tr>
    

    如果您想检查一个变量,而不是像这样硬编码 1007:

    <th:block th:with="check=1007">
      <td th:text="${protocolVersions.^[id==#root.check].name}"></td>
    </th:block>
    

    【讨论】:

    • 另外,这是在
      我发现了!我缺少括号th:with=(${doc.number})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2023-03-30
    • 2015-09-09
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多