Thymeleaf检查数组是否包含字符串

时间:2020-07-07 01:02:07

标签: thymeleaf

我想检查数组是否包含某个字符串,并根据其是否存在进行渲染。

testResponse.test1.itemTypes = ['here','nothere']

<div th:if="${testResponse!= null AND testResponse.test1.itemTypes == 'here'}">
<ul>
    <li><span>I'm here</span></li>
</ul>

1 个答案:

答案 0 :(得分:1)

您可以使用Thymeleaf #arrays.contains()表达式-请参阅文档here

因此,以您的示例为例:

<div th:if="${testResponse != null 
       and #arrays.contains(testResponse.test1.itemTypes, 'here')}">
    <ul>
        <li><span>I'm here</span></li>
    </ul>
</div>

请注意,and必须小写。 AND无效的Thymeleaf语法。

更新:

对于注释中提到的其他情况,请按如下方式使用not运算符:

<div th:if="${testResponse != null 
       and not #arrays.contains(testResponse.test1.itemTypes, 'here')}">
    <ul>
        <li><span>I'm missing</span></li>
    </ul>
</div>