Enums as dropdown in Primefaces

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

public enum Cities {


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

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


I have the same as in config.faces.xml file



When calling the same in my UI I have below code

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


When I run the build and deploy the application .... I don't get any error, nor do I get a dropdown filled with status codes.


source to share

2 answers

Try it....

In your xhtml:

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


In the 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();




I tried this with 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}" />


In the bean:

public enum StateCode {

    ISTANBUL(34) , 

    private int cityCode = 0;

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

    public int getCityCode(){
        return cityCode;

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;




All Articles