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.
Another option is to use a string comparison:
<div th:if="${ == 'GREEN'}">
    Green is for go.

Free Spring Boot Tutorial - 5 Hours Full Course

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