Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing the enum constants in thymeleaf

I have an enum, Constants:

enum Constants {     ONE,TWO,THREE; } 

How can I compare the enum Constants in Thymeleaf.

Thanks.

like image 359
user3128455 Avatar asked Jul 24 '14 15:07

user3128455


People also ask

Can we compare two enums?

There are two ways for making comparison of enum members :equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.


2 Answers

To compare with an enum constant, use the following code:

th:if="${day == T(my.package.MyEnum).MONDAY}" 
like image 183
Nick Avatar answered Oct 05 '22 22:10

Nick


One more way:

th:if="${constant.name() == 'ONE'}" 

It's shorter but makes compare with string representation, can cause problem while refactoring.

like image 30
TachikomaGT Avatar answered Oct 05 '22 22:10

TachikomaGT