How to check if list is empty using thymeleaf?

HtmlThymeleaf

Html Problem Overview


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

Html Solutions


Solution 1 - Html

You can do as follows:

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

Solution 2 - Html

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 - Html

Or simply:

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

Solution 4 - Html

Instead of negating you could use thymeleafs inverted if unless

<div th:unless="${myList.empty}">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul RajView Question on Stackoverflow
Solution 1 - HtmltaxicalaView Answer on Stackoverflow
Solution 2 - HtmlAlexey NikitenkoView Answer on Stackoverflow
Solution 3 - HtmlZonView Answer on Stackoverflow
Solution 4 - HtmlValerij DoblerView Answer on Stackoverflow