Reading on the web, I found a lot of approaches to use Enums with JSF. But to be honest, I found most of them quite overdone. People are working a lot with own converters, et cetera.
But on my try, it went all without that. What I did:
- just refer to the Enum type property in the bean/object
<h:outputText value="Art des Projekts" />
<h:selectOneMenu value="#{projectsBean.selectedObject.type}"
styleClass="inputfield">
<f:selectItem itemLabel="-- not assigned --" />
<f:selectItem itemLabel="------------" />
<f:selectItems value="#{optionProvider.typeOptions}" />
</h:selectOneMenu>
- and provide al list of Enum values
public final List getTypeOptions() {
List typeOptions = new ArrayList();
for (ProjectType pt : ProjectType.values()) {
typeOptions.add(new SelectItem(pt, pt.toString().toLowerCase()));
}
return typeOptions;
}
That’s it – working without any problem.
But now I am wondering, what the reason is, why all the other people make so much effort? Did I miss something? Or is my case just to trivial?