【问题标题】:How to check if a list is not contains a value in Thymeleaf?如何检查列表是否不包含 Thymeleaf 中的值?
【发布时间】:2020-09-18 20:52:20
【问题描述】:

我可以检查一个列表是否包含一个值,比如;

${#lists.contains(data, '1')}

但是,如何检查列表是否包含值?

【问题讨论】:

  • 你试过${!#lists.contains(data, '1')}吗?
  • @wimDeblauwe 是的,但是没有用

标签: list thymeleaf


【解决方案1】:

您可以使用! 反转条件。

假设你的控制器中有这个:

model.addAttribute("data", List.of(1));

那么你可以这样做:

<div th:if="${#lists.contains(data, 1)}">1 is present</div>
<div th:if="${!#lists.contains(data, 2)}">2 is not present</div>

这将输出:

1 存在

2 不存在

注意,我没有引用数字,否则,它是一个字符串。

【讨论】:

  • 这里还有一个微妙的问题:String vs char:见this question。如果您使用的是 Thymeleaf(带有 OGNL),而不是 Spring (SpEL),则这是相关的,在 Spring (SpEL) 中,您有一个 单字符字符串文字,例如此问题中使用的 '1' 示例。
猜你喜欢
  • 2012-08-14
  • 2019-04-04
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 2020-02-02
  • 1970-01-01
相关资源
最近更新 更多