Thymeleaf - Use Enum in If Statements

In this article, we will discuss how to use Enums in an if statement in the Thymeleaf HTML template with an example.

Use Enum in If Statements

We can use a Thymeleaf if statement with our Color enum to conditionally display text:
<div th:if="${tomato.color == T(com.example.thymeleaf.model.Color).RED}">
    Tomato color.
</div>
Another option is to use a string comparison:
<div th:if="${tree.color.name() == 'GREEN'}">
    Green is for go.
</div>

Comments