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>

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course