public class GenericEnumConverter extends Object implements Converter
The omnifaces.GenericEnumConverter
is intended for use in UISelectMany
components whose value is
been bound to a List<E>
property where E
is an enum. Even though JSF has already a
built-in EnumConverter
, this doesn't work for a List<E>
property as the generic type
information E
is lost during runtime. The list would be filled with unconverted String
values instead which may in turn cause ClassCastException
during postprocessing in the business logic.
This can be solved by using a E[]
property instead of List<E>
(e.g.
Role[]
in case of a Role
enum). 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 UISelectMany
with a
List<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.
This converter is available by converter ID omnifaces.GenericEnumConverter
. Just specify it in the
converter
attribute 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>
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
Constructor and Description |
---|
GenericEnumConverter() |
Modifier and Type | Method and Description |
---|---|
Object |
getAsObject(FacesContext context,
UIComponent component,
String submittedValue) |
String |
getAsString(FacesContext context,
UIComponent component,
Object modelValue) |
public String getAsString(FacesContext context, UIComponent component, Object modelValue)
getAsString
in interface Converter
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue)
getAsObject
in interface Converter
Copyright © 2012–2016 OmniFaces. All rights reserved.