Search code examples
javascriptthymeleaf

ThymeLeaf: Not Equal expression in th:if


Very new to ThymeLeaf, but have encountered a problem on a project I am working on. Getting the following error in the logs:

Exception evaluating SpringEL expression: "!searchResults.results.empty"

Looking at the offending code it is:

<th:block th:if="${!searchResults.results.empty}">

I assume that the placement of the exclamation mark (!) is incorrect. I have tried:

<th:block th:if="${not searchResults.results.empty}">

But same error evaluating. Can someone help me as to how to negate a check?


Solution

  • Assuming from the code you've pasted, you want to implement a check where Thymeleaf checks for empty value in an Object. For that :---

    <div th:if= "${searchResults.results != null}">
    

    OR

     <div th:if= "${searchResults.results != ''}">
    

    Also, Alternatively What you can do is-- check on your Controller itself whether the object is empty or doesn't have any Value and send the response on your html page and then check as per that response on Thymeleaf, Like this :- - -

    1.) Your controller :--

        List ls = //some data from you DAO
        if(ls.isEmpty()){
             model.addAttribute("response", "NoData");
        }else{
             model.addAttribute("response", ls);
        }
    

    Then on your Thymeleaf :- - -

    <th:block th:if="${response=='NoData'}"> No Data Found </th:block>