Class GenericEnumConverter
- java.lang.Object
-
- org.omnifaces.converter.GenericEnumConverter
-
public class GenericEnumConverter extends Object implements Converter<Enum>
The
omnifaces.GenericEnumConverteris intended for use inUISelectManycomponents whose value is been bound to aList<E>property whereEis an enum. Even though Faces has already a built-inEnumConverter, this doesn't work for aList<E>property as the generic type informationEis lost during runtime. The list would be filled with unconvertedStringvalues instead which may in turn causeClassCastExceptionduring postprocessing in the business logic.This can be solved by using a
E[]property instead ofList<E>(e.g.Role[]in case of aRoleenum). If this is however is not an option due to some 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); } }<h:selectManyCheckbox value="#{bean.selectedRoles}" converter="roleConverter"> <f:selectItems value="#{bean.availableRoles}" /> </h:selectManyCheckbox>However, creating a new converter for every single enum type, only and only for use in
UISelectManywith aList<E>property, may be a bit clumsy. This generic enum converter is intended to remove the need to create a new enum converter every time.Usage
This converter is available by converter ID
omnifaces.GenericEnumConverter. Just specify it in theconverterattribute of the multi-selection component holding<f:selectItems>. example:<h:selectManyCheckbox value="#{bean.selectedEnums}" converter="omnifaces.GenericEnumConverter"> <f:selectItems value="#{bean.availableEnums}" /> </h:selectManyCheckbox>See also:
Use enum in <h:selectManyCheckbox>JSF 2.3
This converter is not necessary anymore since JSF 2.3 thanks to the fixes in issue 1422.
<h:selectManyCheckbox value="#{bean.selectedEnums}"> <f:selectItems value="#{bean.availableEnums}" /> </h:selectManyCheckbox>However, when you're having an input component without a value attribute, and thus the exact type cannot be automatically determined by simply inspecting the return type of the associated getter method, then this converter may be still useful.
<h:selectManyCheckbox converter="omnifaces.GenericEnumConverter"> <f:selectItems value="#{bean.availableEnums}" /> </h:selectManyCheckbox>- Since:
- 1.2
- Author:
- Bauke Scholtz
-
-
Field Summary
-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description GenericEnumConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EnumgetAsObject(FacesContext context, UIComponent component, String submittedValue)StringgetAsString(FacesContext context, UIComponent component, Enum modelValue)
-
-
-
Method Detail
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Enum modelValue)
- Specified by:
getAsStringin interfaceConverter<Enum>
-
getAsObject
public Enum getAsObject(FacesContext context, UIComponent component, String submittedValue)
- Specified by:
getAsObjectin interfaceConverter<Enum>
-
-