I want to use a selectOneMenu
dropdown with Enum
values. I get to display the values fine, but I'm not able to pass the selected value back to the bean.
Enum
public enum Color {
RED("Red"), BLUE("Blue"), GREEN("Green");
private String label;
private Color(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
Bean1
@ManagedBean
@ApplicationScoped
public class Data {
public Color[] getColors() {
return Color.values();
}
}
Bean2
@ManagedBean(name="bean")
@SessionScoped
public class TestBean {
private Color color;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void submit() {
System.out.println(color.getLabel());
}
}
xhtml
<h:body>
<h:form>
<h:selectOneMenu value="bean.color">
<f:selectItems value="#{data.colors}" var="color" itemValue="#{color}" itemLabel="#{color.label}"/>
</h:selectOneMenu>
<h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>
<h:outputText value="#{bean.color}" />
</h:body>
On submit I get a 500 caused by NullPointerException
in the submit()
function. The problem is bean.color
doesn't get initialized with the selected value.
I'm sure it's something basic though...