如何使用 thymeleaf 检查列表是否为空?

新手上路,请多包涵
<div th:if="${tblUserList != null}">
 --content--
</div>

上面的 thymeleaf 代码不起作用,其中 tblUserList 是一个列表。所以我想检查列表是否为空而不是检查它的空值。怎么做?

原文由 Rahul Raj 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 688
2 个回答

您可以执行以下操作:

 <div th:if="${not #lists.isEmpty(tblUserList)}">
 --content--
</div>

原文由 taxicala 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用 Thymeleaf 3.xx ,您可以更优雅地验证列表:

 <div th:if="${tblUserList!=null and !tblUserList.empty}"></div>

要么

<div th:if="${tblUserList!=null and !tblUserList.isEmpty()}"></div>

原文由 Alexey Nikitenko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题