如何使用百里香检查列表是否为空?

Rah*_*Raj 47 html html5 thymeleaf

<div th:if="${tblUserList != null}">
 --content--
</div>
Run Code Online (Sandbox Code Playgroud)

上面的百万美元代码不起作用,其中tblUserList是一个列表.所以我想检查列表是否为空而不是检查它的null.怎么做?

tax*_*ala 91

你可以这样做:

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

  • `<div th:unless ="$ {#lists.isEmpty(tblUserList)}">`在我看来更具可读性. (9认同)

Ale*_*nko 39

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

<div th:if="${tblUserList!=null and !tblUserList.empty}"></div>
Run Code Online (Sandbox Code Playgroud)

要么

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

  • 如果`tblUserList`为空怎么办? (2认同)

Zon*_*Zon 5

或者简单地:

<div th:if="${!myList.empty}">
Run Code Online (Sandbox Code Playgroud)