Java Reference
In-Depth Information
ject)
methods to help the JavaServer Faces implementation save and restore the full
state of components across multiple requests.
To save a set of values, you can implement the
saveState(FacesContext)
method.
This method is called during the Render Response phase, during which the state of the
response is saved for processing on subsequent requests. Here is a hypothetical method
from
MapComponent
, which has only one attribute,
current
:
@Override
public Object saveState(FacesContext context) {
Object values[] = new Object[2];
values[0] = super.saveState(context);
values[1] = current;
return (values);
}
This method initializes an array, which will hold the saved state. It next saves all of the
state associated with the component.
A component that implements
StateHolder
may also provide an implementation
for
restoreState(FacesContext, Object)
, which restores the state of the
component to that saved with the
saveState(FacesContext)
method. The
re-
storeState(FacesContext, Object)
method is called during the Restore View
phase, during which the JavaServer Faces implementation checks whether there is any
state that was saved during the last Render Response phase and needs to be restored in
preparation for the next postback.
Here is a hypothetical
restoreState(FacesContext, Object)
method from
MapComponent
:
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
current = (String) values[1];
}
This method takes a
FacesContext
and an
Object
instance, representing the array
that is holding the state for the component. This method sets the component's properties
to the values saved in the
Object
array.