EnumConverter


Today i found a very simple solution for using a Enumeration with a converter in JSF. Other than Rick Hightowers or Dave Brondsena i used the hidden javax.faces.convert.EnumConverter. Why hidden? I can’t find no reference in the JSF 1.2 documentation. But both implementations MyFaces and SunRI contains such a class. And the class is not depreacated. So … let’s use it🙂

public enum EnumType {
   A1, A2, A3, A4,
}
public class EnumTypeConverter extends EnumConverter {
  public EnumTypeConverter() {
    super(EnumType.class);
  }
}
  <converter>
    <converter-id>ENUM</converter-id>
    <converter-class>de.ahoehma.jsf.converter.EnumTypeConverter</converter-class>
  </converter>
<h:selectOneMenu id="type" value="#{bean.type}">
  <f:converter converterId="ENUM"/>
  <f:selectItem itemLabel="Type A1" itemValue="A1"/>
  <f:selectItem itemLabel="Type A2" itemValue="A2"/>
  <f:selectItem itemLabel="Type A3" itemValue="A3"/>
  <f:selectItem itemLabel="Type A4" itemValue="A4"/>
</h:selectOneMenu>