[英]Best practice of retrieving the String value of an Enum on the webpage using EL
My object: 我的对象:
public class Account(){
private String accountName;
private AccountType accountType; // enum
//I customized the getter by doing this...
public String getAccountType(){
return accountType.getAccountType();
}
}
My AccountType Enum: 我的AccountType枚举:
public enum AccountType{
OLD("Old account"),
NEW("New account");
private final String accountType;
private AccountType(String accountType){
this.accountType = accountType;
}
public String getAccountType(){
return accountType;
}
}
I use ${account.accountType}
to retrieve the value of the enum constant. 我使用
${account.accountType}
来检索枚举常量的值。 Is this the correct way to do it? 这是正确的方法吗?
I tried using AccountType.valueOf("OLD")
but it returned OLD
. 我尝试使用
AccountType.valueOf("OLD")
但返回OLD
。
What's the best practice for things like these? 这些事情的最佳实践是什么?
Change your enum class like this; 像这样更改您的枚举类;
public enum AccountType{
OLD {
public String type() {
return "Old account";
}
},
NEW {
public String type() {
return "New account";
}
};
}
and your Account object like this; 和您的Account对象是这样的;
public class Account(){
private String accountName;
private AccountType accountType; // enum
//You don't need this.
//public String getAccountType(){
// return accountType.getAccountType();
// }
}
Then you can access accountType.type
然后,您可以访问
accountType.type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.