74
<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?

Rahul Raj
  • 3,197
  • 5
  • 35
  • 55

4 Answers4

143

You can do as follows:

<div th:if="${not #lists.isEmpty(tblUserList)}">
 --content--
</div>
taxicala
  • 21,408
  • 7
  • 37
  • 66
56

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>
Alexey Nikitenko
  • 2,047
  • 2
  • 20
  • 30
4

Or simply:

<div th:if="${!myList.empty}">
Zon
  • 18,610
  • 7
  • 91
  • 99
1

Instead of negating you could use thymeleafs inverted if unless

<div th:unless="${myList.empty}">
Valerij Dobler
  • 1,848
  • 15
  • 25