在Thymeleaf中使用instanceof

时间:2015-02-28 15:19:27

标签: java spring spring-boot thymeleaf spring-el

有没有办法在Thymeleaf中使用Java instanceof运算符?

类似的东西:

<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>

1 个答案:

答案 0 :(得分:21)

尝试:

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

或者,如果使用Spring:

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

有关using SpELdialects in thymeleaf的更多信息。