2012-03-28 109 views
3

我也在努力同樣讓它們在枚舉「CityCodes.java」這是我的枚舉類在那裏我有如下的定義中定義的城市代碼:枚舉爲下拉列表中Primefaces

public enum Cities { 

AL("Alabama","1"), 
AK("Alaska","2"), 
     ....... 
WY("Wyoming","51"); 

    --------------------------------------------------- 
    ******** My managed bean definition************* 
    --------------------------------------------------- 

public class CityCodes {          
    public Cities[] getCityCodes(){ 
    return Cities.values(); 
} 

我也有同樣的config.faces.xml

<managed-bean> 
<managed-bean-name>cityCodes</managed-bean-name> 
<managed-bean-class>com.web.form.CityCodes</managed-bean-class> 
<managed-bean-scope>session</managed-bean-scope> 
</managed-bean> 

在呼籲在我的UI同樣定義我的代碼如下

<h:outputText value="#{msg.stateName}" /> 
<p:selectOneMenu value="#{addressForm.stateCode}"> 
    <f:selectItems itemLabel="#{cityCodes.getCityCodes}" /> 
</p:selectOneMenu> 

當我運行構建和部署應用程序....我沒有得到任何錯誤,我也沒有得到填充狀態代碼的下拉列表。

回答

1

我想這使用JSF 2.

在XHTML:

select state: 
<p:selectOneMenu value="#{enumSelect.selectedCode}"> 
    <f:selectItem itemLabel="Select State" /> 
    <f:selectItems var="state" value="#{enumSelect.stateCodes}" 
        itemValue="#{state}" itemLabel="#{state.name()} - #{state.cityCode}" /> 
</p:selectOneMenu> 

在豆:

public enum StateCode { 

    ISTANBUL(34) , 
    ANKARA(6), 
    IZMIR(35); 

    private int cityCode = 0; 

    private StateCode(int cityCode) { 
     this.cityCode = cityCode; 
    } 

    public int getCityCode(){ 
     return cityCode; 
    } 
} 

@ManagedBean(name="enumSelect") 
public class EnumSelectOneMenu { 

    private StateCode selectedCode; 

    public StateCode[] getStateCodes(){ 
     return StateCode.values(); 
    } 

    public StateCode getSelectedCode() { 
     return selectedCode; 
    } 

    public void setSelectedCode(StateCode selectedCode) { 
     this.selectedCode = selectedCode; 
    } 
} 
2

試試這個....

在你的XHTML:

<p:selectOneRadio id="myRadio" value="#{myBean.selectedState}"> 
    <f:selectItems value="#{myBean.statesToPick}"/> 
</p:selectOneRadio> 

在你的bean:

public stateToPick selectedState; 

public enum stateToPick { 
STATE_1 ("S1"), STATE_2 ("S2"), STATE_3 ("S3"), STATE_4 ("S4"), STATE_5 ("S5"); 
private String value; 
private stateToPick (String value) { this.value = value;} 
} 
public stateToPick statesToPick[] = stateToPick.values(); 
+0

非常感謝的快速反應,這真的幫助我....我試用過的方法,他們都工作...... :) – mainhoonnaa 2012-03-29 21:44:01

+0

高興的是,我可以幫你 – rags 2012-03-30 06:28:08