How to check null and empty condition using Thymeleaf in one single operation?
Try ${#strings.isEmpty(variable)}
.
From Tutorial | Using Thymeleaf | Strings:
/*
* Check whether a String is empty (or null). Performs a trim() operation before check
*/
${#strings.isEmpty(name)}
In order to check null or empty string using thymeleaf expressions, Use this approach : ---
<div th:if= "${searchResults.results != null}">
OR, this :--
<div th:if= "${searchResults.results != ''}">
Furthermore, you can check the empty or null object on your controller itself and then send the response on your thymeleaf-html page accordingly, like this :--
1.) Your Controller :-
List ls = //some data from you DAO
if(ls.isEmpty()){
model.addAttribute("response","NoData");
}else{
model.addAttribute("response",ls);
}
2.) Then on your Thymleaf page :- - -
<th:block th:if="${response=='NoData'}"> No Data Found </th:block>
PS - I've answered the same question here which helps the questioner hope it helps you as well :-- ThymeLeaf: Not Equal expression in th:if