Project Documentation
Foundation

Description

t:saveState enables you to persist beans and values longer than request scope, but shorter than session scope. It is ideal for persisting backing beans and values with the same scope as your view components. It does this by saving the target state with the component tree.

Traditional JSP Applications save their state information within HttpSession objects. This is an easy to use but not always satisfying approach:

  • HttpSession objects must have an expiration date (timeout) that prevents the server from running out of memory over the time. The always boring question for web admins: How long should I set the session timout?
  • Server restarting is a thrilling task: Will all objects in active sessions be serialized and restored successfully? If business classes have changed in the meantime, this task is likely to fail.
  • Running multiple redundant servers is a challenge. You must use cluster environments which are expensive and difficult to handle.

MyFaces supports a different approach. It makes it possible to write sophisticated applications without any use of the HttpSession. All state information of the current view and the model beans can be encoded to the client response. MyFaces introduces a new Component "UISaveState" with the corresponding Tag <t:saveState>.
Example (see "sample1.jsp" of the "examples" web application):

<t:saveState id="save1" value="#{calcForm.number1}" />
<t:saveState id="save2" value="#{calcForm.number2}" />
<t:saveState id="save3" value="#{ucaseForm.text}" />
            

The current values of the three properties number1, number2 and text are automatically saved within the client response and get restored at the next client request.

You can also save the whole bean.
Example:

<t:saveState id="saveCalcForm" value="#{calcForm}"/>
            

The whole bean automatically is saved and restored by MyFaces. To be able to save and restore the value of a bean property or the bean itself, it must implement the Serializable interface.