1

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?

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
  • You can use ``th:unless`` – Alexander Jul 03 '18 at 11:07
  • 1
    Another option is "searchResults.results.empty == false" and @Alexander's th:unless. But I think the error occures because "searchResults.results.empty" can't be evaluated. Is empty a property and does a getter isEmpty() or getEmpty() exists? – Flocke Jul 03 '18 at 11:07
  • Yeah @Flocke, that was exactly it. Further into the logs `Property or field 'empty' cannot be found on null`. Sumit's null check fixed it! – MeanwhileInHell Jul 03 '18 at 12:19

1 Answers1

5

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>
Mike
  • 3
  • 3
Sumit
  • 917
  • 1
  • 7
  • 18