Comparing the enum constants in thymeleaf

前端 未结 10 2047
自闭症患者
自闭症患者 2021-02-02 06:25

I have an enum, Constants:

enum Constants {
    ONE,TWO,THREE;
}

How can I compare the enum Constants in Thymeleaf.

Thank

10条回答
  •  不知归路
    2021-02-02 07:26

    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().

提交回复
热议问题