3

I am also trying the same to get the City codes which are defined in the enum "CityCodes.java" which is my enum class where I have the definition as below:

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();
}   

I have the same defined in 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>

While calling the same in my UI I have the code as below

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

When I run the build and deploy the app....I don't get any error also I don't get the dropdown populated with the State Codes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
mainhoonnaa
  • 45
  • 1
  • 2
  • 5

2 Answers2

2

Try this ....

In your xhtml:

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

In your 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();
rags
  • 2,580
  • 19
  • 37
  • Thanks a lot for the quick response, this really help me....I tried both the approaches and they both work..... :) – mainhoonnaa Mar 29 '12 at 21:44
1

I tried this using jsf 2.

In 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>

In the bean:

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;
    }
}
Anarki
  • 373
  • 5
  • 20
utkusonmez
  • 1,486
  • 15
  • 22