Comparing the enum constants in thymeleaf

前端 未结 10 2049
自闭症患者
自闭症患者 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:09
    th:if="${#strings.toString(someObject.constantEnumString) == 'ONE'}">
    
    0 讨论(0)
  • 2021-02-02 07:15

    Try this :

    th:if="${ day.toString() == 'MONDAY'}"
    
    0 讨论(0)
  • 2021-02-02 07:21

    One more way:

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

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

    0 讨论(0)
  • 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().

    0 讨论(0)
提交回复
热议问题