So I am creating a web application which should display a dropdown box with all values from an enum. I found this question which does provide all the info I would need. Sadly, I can't use the code like this, as it throws errors.
First, the enum:
public enum Status {
YES("Yes"), NO("No"), OPT_IN("Opt in");
private String label;
Status(String label) {
this.setLabel(label);
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
And now the xhtml page:
<p:importEnum type="package.name.Status"/>
<p:outputLabel for="statusDropdown" value="Status:" />
<p:selectOneMenu id="statusDropdown"
value="#{model.status}" >
<f:selectItems value="#{Status}" var="statusEnum"
itemValue="#{statusEnum}" itemLabel="#{statusEnum.label}"/>
</p:selectOneMenu>
According to the question I linked, a dropdown with the enum values and the label
string should show. However, this is not the case.
With itemLabel
usage, I get an error saying that 'label' is not a valid enum value.
Abbreviated stacktrace:
java.lang.NumberFormatException: For input string: "label"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:144)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:61)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.UIComponentBase$AttributesMap.get(UIComponentBase.java:2427)
at org.primefaces.renderkit.SelectRenderer.createSelectItem(SelectRenderer.java:161)
at org.primefaces.renderkit.SelectRenderer.getSelectItems(SelectRenderer.java:114)
And when removing the 'itemLabel' I get another error:
javax.faces.convert.ConverterException: Status: '[Lpackage.name.Status;@42b3ca25' must be convertible to an enum.
at javax.faces.convert.EnumConverter.getAsString(EnumConverter.java:219)
at org.primefaces.renderkit.SelectRenderer.getOptionAsString(SelectRenderer.java:203)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeOption(SelectOneMenuRenderer.java:591)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeSelectItems(SelectOneMenuRenderer.java:554)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeHiddenSelect(SelectOneMenuRenderer.java:241)
at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeInput(SelectOneMenuRenderer.java:207)
I have debugged this error and found that besides the 3 enum values, it tries to add a 4th value. This 4th value is 'ALL_VALUES' array.
In a last test I just changed <p:selectOneMenu>
to <h:selectOneMenu>
and now it does kind of work. It still adds an ALL_VALUES
selection option which it just shouldn't do.
Any help is appreciated to get a dropdown with just the enum values and preferably the correct label as well.
Thanks
The list of imported enums, from Primefaces importEnum
component, is managed by ImportEnumTagHandler
class.
As you can see in getEnumValues
function, it iterates over Enum Constants
of each types and then add in the map another value, with the list of each type, using the suffix specified in the xhtml (default value is ALL_VALUES
).
So, if you have only one enum and you don't specify any suffix, you can use the enum for the selectItems like this:
<p:importEnum type="package.Status" />
<p:outputLabel for="statusDropdown" value="Status:" />
<p:selectOneMenu id="statusDropdown" value="#{bean}">
<f:selectItems value="#{Status.ALL_VALUES}"
itemLabel="#{element.label}" itemValue="#{element}" var="element" />
</p:selectOneMenu>