org.omnifaces.converter
Class GenericEnumConverter

java.lang.Object
  extended by org.omnifaces.converter.GenericEnumConverter
All Implemented Interfaces:
javax.faces.convert.Converter

public class GenericEnumConverter
extends java.lang.Object
implements javax.faces.convert.Converter

This generic enum converter is intended for use in UISelectMany components whose value is been bound to a List<T> property. Even though JSF has already a built-in EnumConverter, this doesn't work for a List<T> property as the generic type information T is lost during runtime. The list would be filled with unconverted String values instead which may in turn cause ClassCastException.

If replacing the List<T> property by a T[] (e.g. Role[] in case of a Role enum) is not an option due to design restrictions (e.g. JPA @ElementCollection, etc), then you'd need to create an explicit converter for the enum type like follows:

 @FacesConverter("roleConverter")
 public class RoleConverter extends EnumConverter {
     public RoleConverter() {
         super(Role.class);
     }
 }
 

However, creating a new converter for every single enum type, only and only for use in UISelectMany with a List<T> property, may be a bit clumsy. This generic enum converter is intended to remove the need to create a new enum converter every time.

This converter is available by converter ID omnifaces.GenericEnumConverter. Basic usage example:

 <h:selectManyCheckbox value="#{bean.selectedEnums}" converter="omnifaces.GenericEnumConverter">
   <f:selectItems value="#{bean.availableEnums}" />
 </h:selectManyCheckbox>
 

Since:
1.2
Author:
Bauke Scholtz

Constructor Summary
GenericEnumConverter()
           
 
Method Summary
 java.lang.Object getAsObject(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.String submittedValue)
           
 java.lang.String getAsString(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.Object modelValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericEnumConverter

public GenericEnumConverter()
Method Detail

getAsString

public java.lang.String getAsString(javax.faces.context.FacesContext context,
                                    javax.faces.component.UIComponent component,
                                    java.lang.Object modelValue)
Specified by:
getAsString in interface javax.faces.convert.Converter

getAsObject

public java.lang.Object getAsObject(javax.faces.context.FacesContext context,
                                    javax.faces.component.UIComponent component,
                                    java.lang.String submittedValue)
Specified by:
getAsObject in interface javax.faces.convert.Converter