Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 partial state saving does not seem to work

I am evaluating the possibility of using JSF in a high-traffic website. I am told that in JSF 2.0 the component tree is not stored in the session, and that only deltas are stored once the component tree is modified.

Here is the page I am viewing:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://java.sun.com/jsf/html">
    <body>
        <h:form>
            hello, world
        </h:form>
    </body>
</html>

Every time I view this page, nearly 1K is allocated to the session. If I remove the <form> tag, nothing is stored in the session.

Any idea why the component tree is being stored in the session? I would think that this would be computed on the postback request.

like image 939
SamBeran Avatar asked Dec 08 '10 18:12

SamBeran


1 Answers

Partial state saving does not mean that the state won't be saved in the session. It only means that a part of the component tree state will be saved instead of the entire component tree state. The key idea of partial state saving is that the state of components which wouldn't be changed by the client side in the subsequent request won't be saved. Instead, it is obtained by re-executing the view on the server side during restore view. Only the component state which is sensitive to changes by the client (forms, inputs, buttons, etc) will be saved. The 1K which you see in session is the partial state itself.

To test it yourself, toggle the state on and off by the following context-param in web.xml:

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

You'll see that the size increases when the setting is false, which means that the entire component tree is been saved instead.

It is stored in session because that's the only provided by the Servlet API which has a larger scope than the request scope. Storing in request scope would have no value since it wouldn't be available anymore on the subsequent request. The Servlet API has no notion of the view scope like as JSF has (which is under the covers indirectly using the session scope by the way, basically, the view state is the component tree state).

You indeed don't see it anymore when you remove the form since there's actually nothing left which the client could change (i.e. there would be no postback). It would not make sense to save the state then. Besides, there would be nothing to pass the key of the saved state as a hidden input field (with the name javax.faces.ViewState).

See also:

  • What's new in JSF 2.0? - State saving
  • What's the view build time?
  • Why JSF saves the state of UI components on server?
like image 156
BalusC Avatar answered Oct 16 '22 16:10

BalusC