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 {
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 as in config.faces.xml file
<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>
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}" />
</p:selectOneMenu>
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.
+3
source to share
2 answers
Try it....
In your xhtml:
<p:selectOneRadio id="myRadio" value="#{myBean.selectedState}">
<f:selectItems value="#{myBean.statesToPick}"/>
</p:selectOneRadio>
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();
+2
source to share
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}" />
</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;
}
}
+1
source to share