org.omnifaces.converter
Class SelectItemsConverter

java.lang.Object
  extended by org.omnifaces.converter.SelectItemsConverter
All Implemented Interfaces:
javax.faces.convert.Converter

public class SelectItemsConverter
extends java.lang.Object
implements javax.faces.convert.Converter

Base class for a converter that automatically converts a string representation of an Object back to its original object based on the associated select items for the component for which conversion is taking place.

This class can be used as-is provided the Object has a toString method that uniquely identifies it and which makes sense as an identifier. Since this is not the typical case for toString, users are assumed to override the getAsString method and provide an implementation that converts an object to its string representation. This getAsObject method will then be used to automatically do the reverse mapping.

This converter is available by converter ID omnifaces.SelectItemsConverter. Basic usage example:

 <h:selectOneMenu value="#{bean.selectedEntity}" converter="omnifaces.SelectItemsConverter">
   <f:selectItems value="#{bean.availableEntities}" var="entity"
     itemValue="#{entity}" itemLabel="#{entity.someProperty}" />
 </h:selectOneMenu>
 

When extending this converter to override getAsString() to return something simpler, such as the entity ID by return String.valueOf(((Entity) value).getId());, you only have to change the converter attribute in the above example to refer the extended converter instead.

Author:
Arjan Tijms

Constructor Summary
SelectItemsConverter()
           
 
Method Summary
 java.lang.Object getAsObject(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.String value)
           
 java.lang.String getAsString(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelectItemsConverter

public SelectItemsConverter()
Method Detail

getAsObject

public java.lang.Object getAsObject(javax.faces.context.FacesContext context,
                                    javax.faces.component.UIComponent component,
                                    java.lang.String value)
Specified by:
getAsObject in interface javax.faces.convert.Converter

getAsString

public java.lang.String getAsString(javax.faces.context.FacesContext context,
                                    javax.faces.component.UIComponent component,
                                    java.lang.Object value)
Specified by:
getAsString in interface javax.faces.convert.Converter