-
Available since OmniFaces 1.2
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 Faces 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.
Usage
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>
Since OmniFaces 4.5 it's also available by <o:genericEnumConverter>
tag.
<h:selectManyCheckbox value="#{bean.selectedEnums}">
<f:selectItems value="#{bean.availableEnums}" />
<o:genericEnumConverter />
</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>
With ExampleEnum[]
- works fine
With List<ExampleEnum>
- wrong type has been set!
With List<ExampleEnum>
and omnifaces.GenericEnumConverter
- works fine
<o:importConstants type="org.omnifaces.showcase.model.ExampleEnum" />
<h3>With <code>ExampleEnum[]</code> - works fine</h3>
<h:form>
<h:selectManyCheckbox value="#{genericEnumBean.enumArray}">
<f:selectItems value="#{ExampleEnum}" />
<f:ajax render="showSelected" />
</h:selectManyCheckbox>
<p>Selected:
<h:panelGroup id="showSelected">
<ui:repeat value="#{genericEnumBean.enumArray}" var="item" varStatus="loop">
#{item} (#{item['class'].simpleName})#{loop.last ? '' : ','}
</ui:repeat>
</h:panelGroup>
</p>
</h:form>
<hr />
<h3>With <code>List<ExampleEnum></code> - wrong type has been set!</h3>
<h:form>
<h:selectManyCheckbox binding="#{selectManyCheckbox1}">
<f:selectItems value="#{ExampleEnum}" />
<f:ajax render="showSelected" />
</h:selectManyCheckbox>
<p>Selected:
<h:panelGroup id="showSelected">
<ui:repeat value="#{selectManyCheckbox1.value}" var="item" varStatus="loop">
#{item} (#{item['class'].simpleName})#{loop.last ? '' : ','}
</ui:repeat>
</h:panelGroup>
</p>
</h:form>
<hr />
<h3>With <code>List<ExampleEnum></code> and <code>omnifaces.GenericEnumConverter</code> - works fine</h3>
<h:form>
<h:selectManyCheckbox binding="#{selectManyCheckbox2}" converter="omnifaces.GenericEnumConverter">
<f:selectItems value="#{ExampleEnum}" />
<f:ajax render="showSelected" />
</h:selectManyCheckbox>
<p>Selected:
<h:panelGroup id="showSelected">
<ui:repeat value="#{selectManyCheckbox2.value}" var="item" varStatus="loop">
#{item} (#{item['class'].simpleName})#{loop.last ? '' : ','}
</ui:repeat>
</h:panelGroup>
</p>
</h:form>
package org.omnifaces.showcase.converters;
import java.io.Serializable;
import jakarta.inject.Named;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.showcase.model.ExampleEnum;
@Named
@ViewScoped
public class GenericEnumBean implements Serializable {
private static final long serialVersionUID = 1L;
private ExampleEnum[] enumArray;
public ExampleEnum[] getEnumArray() {
return enumArray;
}
public void setEnumArray(ExampleEnum[] enumArray) {
this.enumArray = enumArray;
}
}
package org.omnifaces.showcase.model;
public enum ExampleEnum {
ONE, TWO, THREE;
public String getFriendlyName() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
}