I have an enum, Constants
:
enum Constants {
ONE,TWO,THREE;
}
How can I compare the enum Constants in Thymeleaf.
Thank
Example #1:
enum Constants {
ONE,
TWO,
THREE;
}
Solution:
th:if="${string == T(com.example.Constants).ONE}"
OR
th:if="${string == T(com.example.Constants).ONE.toString()}"
NB: You can use .toString()
for safety.
Example #2:
enum Constants {
ONE("One"),
TWO("Two"),
THREE("Three");
}
Solution:
th:if="${string == T(com.example.Constants).ONE.toString()}"
NB: When your enum has Description, You have to use .toString()
.
Example #3:
enum Constants {
ONE("One"),
TWO("Two"),
THREE("Three");
}
Solution:
th:if="${'One' == T(com.example.Constants).ONE.getName()}"
NB: Enum description call by .getName()
.