Class ValueChangeConverter<T>
- java.lang.Object
-
- org.omnifaces.converter.ValueChangeConverter<T>
-
- All Implemented Interfaces:
Converter<T>
public abstract class ValueChangeConverter<T> extends Object implements Converter<T>
By default, Faces converters run on every request, regardless of whether the submitted value has changed or not. In case of conversion against the DB on complex objects which are already stored in the model in a broader scope, such as the view scope, this may result in unnecessarily expensive service/DAO calls. In such case, you'd like to perform the expensive service/DAO call only when the submitted value is really changed as compared to the model value.
This converter offers you a template to do it transparently. To use it, just change your converters from:
public class YourConverter implements Converter<YourEntity> { public YourEntity getAsObject(FacesContext context, UIComponent component, String submittedValue) { // ... } // ... }
to
public class YourConverter extends ValueChangeConverter<YourEntity> { public YourEntity getAsChangedObject(FacesContext context, UIComponent component, String submittedValue) { // ... } // ... }
So, essentially, just replace
implements Converter
byextends ValueChangeConverter
and rename the method fromgetAsObject
togetAsChangedObject
. Note: thegetAsString
method of your converter doesn't need to be changed.- Since:
- 1.6
- Author:
- Bauke Scholtz
-
-
Field Summary
-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description ValueChangeConverter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract T
getAsChangedObject(FacesContext context, UIComponent component, String submittedValue)
Use this method instead ofgetAsObject(FacesContext, UIComponent, String)
if you intend to perform the conversion only when the submitted value is really changed as compared to the model value.T
getAsObject(FacesContext context, UIComponent component, String submittedValue)
If the component is an instance ofEditableValueHolder
and the string representation of its old object value is equal to the submitted value, then immediately return its old object value unchanged.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jakarta.faces.convert.Converter
getAsString
-
-
-
-
Method Detail
-
getAsObject
public T getAsObject(FacesContext context, UIComponent component, String submittedValue)
If the component is an instance ofEditableValueHolder
and the string representation of its old object value is equal to the submitted value, then immediately return its old object value unchanged. Otherwise, invokegetAsChangedObject(FacesContext, UIComponent, String)
which may in turn do the necessary possibly expensive DAO operations.- Specified by:
getAsObject
in interfaceConverter<T>
- Throws:
ClassCastException
- WhenT
is of wrong type.
-
getAsChangedObject
public abstract T getAsChangedObject(FacesContext context, UIComponent component, String submittedValue)
Use this method instead ofgetAsObject(FacesContext, UIComponent, String)
if you intend to perform the conversion only when the submitted value is really changed as compared to the model value.- Parameters:
context
- The involved faces context.component
- The involved UI component.submittedValue
- The submitted value.- Returns:
- The converted value, exactly like as when you use
getAsObject(FacesContext, UIComponent, String)
the usual way.
-
-