2015-02-28 110 views
9

Có cách nào để sử dụng toán tử Java instanceof trong Thymeleaf không?Sử dụng instanceof trong Thymeleaf

Cái gì như:

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

Trả lời

16

Hãy thử:

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

hoặc, nếu sử dụng Spring:

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

thêm về using SpELdialects in thymeleaf.

+1

cảm ơn! cả hai phiên bản đều hoạt động tuyệt vời! – Andrea

Các vấn đề liên quan