Home » Html » How to check if list is empty using thymeleaf?

How to check if list is empty using thymeleaf?

Posted by: admin November 30, 2017 Leave a comment

Questions:
<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?

Answers:

You can do as follows:

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

Questions:
Answers:

With Thymeleaf 3.x.x you can validate list more elegant:

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

or

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