- java.lang.Object
-
- org.omnifaces.converter.SelectItemsConverter
-
public class SelectItemsConverter extends Object implements Converter<Object>
The
omnifaces.SelectItemsConverter
allows you to populate a selection component with complex Java model objects (entities) as value of<f:selectItems>
and have Faces convert those automatically back without the need to provide a custom converter which may need to do the job based on possibly expensive service/DAO operations. This converter automatically converts based on theObject.toString()
of the selected item.Usage
This converter is available by converter ID
omnifaces.SelectItemsConverter
. Just specify it in theconverter
attribute of the selection component holding<f:selectItems>
.<h:selectOneMenu value="#{bean.selectedItem}" converter="omnifaces.SelectItemsConverter"> <f:selectItems value="#{bean.availableItems}" /> </h:selectOneMenu>
Since OmniFaces 4.5 it's also available by
<o:selectItemsConverter>
tag.<h:selectOneMenu value="#{bean.selectedItem}"> <f:selectItems value="#{bean.availableItems}" /> <o:selectItemsConverter /> </h:selectOneMenu>
Make sure that your entity has a good
toString()
implementationThe base converter uses by default the
toString()
method of the entity to uniquely identify the instance during the conversion. This is sufficient if your (abstract base) entity has atoString()
implementation which looks something like this:@Override public String toString() { return String.format("%s[id=%d]", getClass().getSimpleName(), getId()); }
By the way, you should also make sure that your entity has a good
equals()
andhashCode()
implementation, otherwise Faces won't be able to set the right entity back in the model. Please note that this problem is in turn unrelated to theSelectItemsConverter
, you would have faced the same problem when using any other converter.If your entity can't have a good
toString()
implementationHowever, if the entity doesn't have a
toString()
implementation (and thus relies on the defaultfqn@hashcode
implementation), or the existing implementation doesn't necessarily uniquely identify the instance, and you can't implement/change it, then it is recommended to extend theSelectItemsConverter
class and override only thegetAsString
method wherein the desired implementation is provided. For example:@FacesConverter("exampleEntitySelectItemsConverter") public class ExampleEntitySelectItemsConverter extends SelectItemsConverter { @Override public String getAsString(FacesContext context, UIComponent component, Object value) { Long id = (value instanceof ExampleEntity) ? ((ExampleEntity) value).getId() : null; return (id != null) ? String.valueOf(id) : null; } }
Again, you do not need to override the
getAsObject()
method which would only need to perform possibly expensive service/DAO operations. TheSelectItemsConverter
base converter will already do it automatically based on the available items and thegetAsString()
implementation.An alternative is to switch to
SelectItemsIndexConverter
, which will convert based on the position (index) of the selected item in the list instead of theObject.toString()
of the selected item.- Author:
- Arjan Tijms
- See Also:
SelectItemsUtils
,SelectItemsCollector
-
-
Field Summary
-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description SelectItemsConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
getAsObject(FacesContext context, UIComponent component, String value)
String
getAsString(FacesContext context, UIComponent component, Object value)
-
-
-
Method Detail
-
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String value)
- Specified by:
getAsObject
in interfaceConverter<Object>
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Object value)
- Specified by:
getAsString
in interfaceConverter<Object>
-
-