enmu基本知識
//簡單的枚舉
public enum Planet {
MERCURY ,
VENUS
}
//負責的枚舉
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6)
private final double mass; // in kilograms
private final double radius; // in meters
//MERCURY (3.303e+23, 2.4397e6)里面的參數與構造函數對應
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getmass() { return mass; }
public
double getradius() { return radius; }
}
在jsf頁面使用Enmu
<h:selectOneMenu id="CustomerStatusList" value="#{customerAccountsAction.status}">
<s:selectItems value="#{customerStatusList}" var="_s" label="#{_s.label}" noSelectionLabel="" />
<s:convertEnum />
</h:selectOneMenu>
@Factory
public CustomerAccountStatus[] getCustomerStatusList() {
return CustomerAccountStatus.values();
}
package org.manaty.model.party.customerAccount;
public enum CustomerAccountStatus {
ACTIVE("Actif"),LOCKED("Verrouill"u00E9"),CLOSED("Ferm"u00E9");
private String label;
CustomerAccountStatus(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
目前存在的問題是label="#{_s.label}"沒法國際化,如果要在列表中國際化就應該 在getCustomerStatusList()
方法中修改枚舉的label值,如此給CustomerAccountStatus增加setLabel方
法,getCustomerStatusList()方法中循環
CustomerAccountStatus.values(),取出枚舉值的name,然后再去資源文件中獲取對應的國際化label。、
問題在于這樣就修改了枚舉值本身的內容,而枚舉值的含義是相當于常量,常量是不可以改變的。
最新解決辦法
不用label來顯示enmu,直接用enmu.name()來作為key去資源文件中取對應的國際化
<h:selectOneMenu id="CustomerStatusList" value="#{customerAccountsAction.status}">
<s:selectItems value="#{customerStatusList}" var="_s" label="#{messages[_s.name()]}
" noSelectionLabel="" />
<s:convertEnum />
</h:selectOneMenu>
為什么使用_s.name()而不使用
_s.label呢?
因為label是特定enmu的方法,,而不是所有enmu有的方法,這樣_s.label就不通用了。
原帖地址: http://yourenyouyu2008.javaeye.com/blog/351703