Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use instanceof in Thymeleaf

Is there a way to use the Java instanceof operator in Thymeleaf?

Something like:

<span th:if="${animal} instanceof my.project.Cat" th:text="A cat"></span>
<span th:if="${animal} instanceof my.project.Dog" th:text="A dog"></span>
like image 584
Andrea Avatar asked Feb 28 '15 15:02

Andrea


1 Answers

Try:

<span th:if="${animal.class.name == 'my.project.Cat'}" th:text="A cat"></span>

or, if using Spring:

<span th:if="${animal instanceof T(my.project.Cat)}" th:text="A cat"></span>

more about using SpEL and dialects in thymeleaf.

like image 138
Trynkiewicz Mariusz Avatar answered Nov 19 '22 14:11

Trynkiewicz Mariusz