17

Maybe is a stupid question but I failed to retrieve information from Google. As the title say, I get a stack trace if a try to parse this simple line:

<span th:if="${1 < 0}">

The error is:

org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 43; The value of attribute "th:if" associated with an element type "null" must not contain the '<' character.

But if i try this:

<span th:if="${0 > 1}">

Everything is fine, my question is: Why I get this error?

I believe is related to my lack of experience with Java and thymeleaf, but I don't get why by just changing the position of the elements it work as I expect (return always false).

It is a bug in the parsing of the expression (as checking if 1 is lower than 0 is forbidden by some parser rule) or is just a weird XML parsing issue?

Thank you to all who will even just read.

Matteo
  • 1,107
  • 2
  • 27
  • 47

3 Answers3

45

You have to escape the symbol by using

&lt; for < 
&gt; for >
&le; for <= 
&ge; for >=

So your code should look like :

<span th:if="${1 &lt; 0}">

You can find the whole doc about this in the 'Using Thymeleaf' tutorial on their website, in the comparators-and-equality section.

Ben
  • 308
  • 1
  • 4
  • 11
brnrd
  • 3,396
  • 3
  • 31
  • 41
15
&le; for <= 
&ge; for >=

didn't work for me. I had to use:

&lt;= for <= 
&gt;= for >=

It seems that &le; and &ge; are not accepted as well-formed XML.

This solves the "IllegalStateException: Cannot handle (8804) '≤' " problem.

Pang
  • 9,564
  • 146
  • 81
  • 122
Maxime G.
  • 151
  • 1
  • 5
  • thanks, any idea ≥ and ≤ not working? I have the same issue. – Stelium Jul 30 '18 at 14:39
  • Because `≤` is not a valid operator in Java, infact you have to use `<=` instead. Maybe `≤` worked long time ago. If you see @brnrd 's answer if from 2013. – FonzTech Nov 24 '19 at 16:00
-1

I have come with same problem, but shows different exception and my code is here:

<div class="text-center m-1" th:if="${totalCount} > 0">

    <span> Showing user # [[${startCount}]] to [[${endCount}]] of [[${totalCount}]] </span>

</div>

Here is my stack trace:

Caused by: org.attoparser.ParseException: Cannot execute GREATER THAN comparison: operands are "null" and "0"

So, I have changed to:

 <div class="text-center m-1" th:if="${totalCount} != 0">

        <span> Showing user # [[${startCount}]] to [[${endCount}]] of [[${totalCount}]] </span>

    </div>

It works.

njpollob
  • 11
  • 1
  • 5
  • You brutally changed a "greater than X" into a "not X" that is not the same thing at all unless you are absolutely sure that the checked value can only produce the desired range of values. Furthermore for the specific case in the example you should look into boolean parsing and thruty conditions in the Smarty template engine. – Matteo Nov 14 '21 at 11:03