- java.lang.Object
-
- org.omnifaces.converter.SelectItemsIndexConverter
-
public class SelectItemsIndexConverter extends Object implements Converter<Object>
The
omnifaces.SelectItemsIndexConverter
is a variant of theSelectItemsConverter
which automatically converts based on the position (index) of the selected item in the list instead of theObject.toString()
of the selected item.Usage
This converter is available by converter ID
omnifaces.SelectItemsIndexConverter
. Just specify it in theconverter
attribute of the selection component holding<f:selectItems>
.<h:selectOneMenu value="#{bean.selectedItem}" converter="omnifaces.SelectItemsIndexConverter"> <f:selectItems value="#{bean.availableItems}" /> </h:selectOneMenu>
Since OmniFaces 4.5 it's also available by
<o:selectItemsIndexConverter>
tag.<h:selectOneMenu value="#{bean.selectedItem}"> <f:selectItems value="#{bean.availableItems}" /> <o:selectItemsIndexConverter /> </h:selectOneMenu>
Pros and cons as compared to
SelectItemsConverter
This converter has the following advantages over
SelectItemsConverter
:- No need to rely on
Object.toString()
method of the object. - No need to extend the
SelectItemsConverter
whenObject.toString()
method of the object cannot be used. - No need to expose the object's unique key in its
Object.toString()
,if that's a problem.
This converter has the following disadvantage over
SelectItemsConverter
:- The "Validation Error: value is not valid" will never occur anymore for the case that the available select items has incompatibly changed during the postback due to a developer's mistake. The developer should make absolutely sure that exactly the same list is preserved on postback (e.g. by making it a property of a view scoped or broader scoped bean).
- Since:
- 1.3
- Author:
- Patrick Dobler, Bauke Scholtz
- See Also:
SelectItemsUtils
,SelectItemsCollector
- No need to rely on
-
-
Field Summary
-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description SelectItemsIndexConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
getAsObject(FacesContext context, UIComponent component, String submittedValue)
String
getAsString(FacesContext context, UIComponent component, Object modelValue)
-
-
-
Method Detail
-
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue)
- Specified by:
getAsObject
in interfaceConverter<Object>
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Object modelValue)
- Specified by:
getAsString
in interfaceConverter<Object>
-
-