I have an enum, Constants
:
enum Constants {
ONE,TWO,THREE;
}
How can I compare the enum Constants in Thymeleaf.
Thank
th:if="${#strings.toString(someObject.constantEnumString) == 'ONE'}">
Try this :
th:if="${ day.toString() == 'MONDAY'}"
One more way:
th:if="${constant.name() == 'ONE'}"
It's shorter but makes compare with string representation, can cause problem while refactoring.
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()
.