我想检查数组是否包含某个字符串,并根据其是否存在进行渲染。
testResponse.test1.itemTypes = ['here','nothere']
<div th:if="${testResponse!= null AND testResponse.test1.itemTypes == 'here'}">
<ul>
<li><span>I'm here</span></li>
</ul>
答案 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>