'How to check if list is empty using thymeleaf?
<div th:if="${tblUserList != null}">
--content--
</div>
The above thymeleaf code is not working, where tblUserList is a list. So I want to check whether the list is empty instead of checking its null. How to do that?
Solution 1:[1]
You can do as follows:
<div th:if="${not #lists.isEmpty(tblUserList)}">
--content--
</div>
Solution 2:[2]
With Thymeleaf 3.x.x you can validate list more elegant:
<div th:if="${tblUserList!=null and !tblUserList.empty}"></div>
or
<div th:if="${tblUserList!=null and !tblUserList.isEmpty()}"></div>
Solution 3:[3]
Or simply:
<div th:if="${!myList.empty}">
Solution 4:[4]
Instead of negating you could use thymeleafs inverted if unless
<div th:unless="${myList.empty}">
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | taxicala |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Valerij Dobler |
