Monday, May 14, 2007

JSF component development - StateHolder Interface

JSF components are rendered upon client request. Clients may make several requests for multiple views. Since the components are statefull, we must save the state of the previous component before rendering another component.

The state of a component can be saved on the server as well as the client. When saved on the server, most likely it is saved in the client's session and nothing special needs to be done by the component developer to enable state saving and restoring. However, when the state is saved on the client, we must implement the methods saveState() and restoreState() defined in the StateHolder Interface.

The saveState() method should return a serializable object (usually Object[]) with all the properties of the component and it's superclass, as well as any state that needs to be saved from the renderers, converters, etc.

The restoreState() method accepts that same serialized object and restores the state of the component by getting property values from it.

Since we take the responsibility of saving and restoring state, these methods should be mirror images.

Note: It is important to test the component with STATE_SAVING_MATHOD=client and server.

No comments: