Java 比较 thymeleaf 中的枚举常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24937441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing the enum constants in thymeleaf
提问by user3128455
I have an enum, Constants
:
我有一个枚举Constants
:
enum Constants {
ONE,TWO,THREE;
}
How can I compare the enum Constants in Thymeleaf.
如何比较 Thymeleaf 中的枚举常量。
Thanks.
谢谢。
回答by user3128455
th:if="${#strings.toString(someObject.constantEnumString) == 'ONE'}">
回答by Nick
To compare with an enum constant, use the following code:
要与枚举常量进行比较,请使用以下代码:
th:if="${day == T(my.package.MyEnum).MONDAY}"
回答by Vegard
If you don't want to convert your Enum into a string in your object you can do it like this:
如果您不想将 Enum 转换为对象中的字符串,您可以这样做:
th:if="${#strings.defaultString(someObject.myConstant, 'DEFAULT_VALUE') == 'ONE'}"
回答by TachikomaGT
One more way:
还有一种方法:
th:if="${constant.name() == 'ONE'}"
It's shorter but makes compare with string representation, can cause problem while refactoring.
它更短,但与字符串表示相比,在重构时可能会导致问题。
回答by geoff.weatherall
@Nick answer has a small syntax error, it's missing a final brace. It should be
@Nick 答案有一个小的语法错误,它缺少最后一个大括号。它应该是
th:if="${day == T(my.package.MyEnum).MONDAY}"
回答by LuiGi
I prefere use wrapper around the enum.
我更喜欢在枚举周围使用包装器。
public class ConstantsWrapper {
public static final instance = new ConstantsWrapper();
public Constants getONE() { return Constants.ONE }
public Constants getTWO() { return Constants.TWO }
public Constants getTHREE() { return Constants.THREE }
}
Next step is adding new instance of Wrapper into models
下一步是将 Wrapper 的新实例添加到模型中
models.add("constantsWrapper", ConstantsWrapper.instance);
Now you have type safe access in thymeleaf template to the each value of Constants
.
现在,您可以在 thymeleaf 模板中键入安全访问Constants
.
th:if="${value == constantsWrapper.getONE()}"
I know that this solutions needs to write more source code and create one instnace.
Advantage is more type safe than call T()
and write full qualified name directly into template. With this solution you can refactor without testing your templates.
我知道这个解决方案需要编写更多的源代码并创建一个实例。Advantage 比调用T()
和直接将全限定名写入模板更类型安全。使用此解决方案,您无需测试模板即可重构。
回答by Pau
Another option is using the method name() of the enum in a switch. An example would be:
另一种选择是在开关中使用枚举的方法 name()。一个例子是:
<th:block th:switch="${imageType.name()}>
<span th:case="'ONE'"></span>
<span th:case="'TWO'"></span>
<span th:case="'THREE'"></span>
</th:block>
It is similar to a Java Switch swith ENUM instead of other answers like ${day == T(my.package.MyEnum).MONDAY}
or ${#strings.toString(someObject.constantEnumString) == 'ONE'}
which looks very strange.
它类似于带有 ENUM 的 Java Switch 开关,而不是其他类似${day == T(my.package.MyEnum).MONDAY}
或${#strings.toString(someObject.constantEnumString) == 'ONE'}
看起来很奇怪的答案。
回答by Dilip Dalwadi
Try this :
尝试这个 :
th:if="${ day.toString() == 'MONDAY'}"
回答by daiscog
I tend to add isXXX()
methods to my enums:
我倾向于在isXXX()
我的枚举中添加方法:
enum Constants {
ONE,TWO,THREE;
public boolean isOne() { return this == ONE; }
public boolean isTwo() { return this == TWO; }
public boolean isThree() { return this == THREE; }
}
Then, if value
is an instance of your enum, you can use th:if="${value.one}"
.
然后,如果value
是枚举的实例,则可以使用th:if="${value.one}"
.