Direct Web Remoting

Propagating Client-Side View State

If your application stores view state on the client and you need access to view state, then you must propagate that state with your Ajax calls.

If you look carefully at the phase listener from the previous example, you will see that we have taken client-side state saving into account. Notice that we invoke a JavaScript function called getJSFState:

  <script type="text/javascript" language="Javascript1.1">   <!--      function zipChanged(zip) {          var jsfState = getJSFState();          new Ajax.Request(             window.document.forms[0].action,             {method: "post",              parameters: "ajax=true&zip=" + zip + "&javax.faces.ViewState=" + jsfState,              onComplete: processRealtimeValidation              });          }          ...     

The getJSFState function returns a string that we pass through to the request as a request parameter named javax.faces.ViewState. That string is the serialized version of the current component tree, which was previously stored by JSF in a hidden field, named javax.faces.ViewState component.

  ...   function getJSFState() {      var state = window.document.getElementsByName("javax.faces.ViewState");      var value = null;      if(null != state && 0 < state.length) {          value = state[0].value;          var encodedValue = encodeURI(value);          var re = new RegExp("\\+", "g");          return encodedValue.replace(re, "\%2B");      }   }     

To get the serialized view state, we access the value of the hidden field with the special name javax.faces.ViewState. Then we escape all plus signs in the string and return it to the zipChanged function, which propagates the state.

If you are saving state on the server, none of this section applies to your application. Still, in general, you should account for client-side state saving so that your application works in both cases.

Note

When you store view state on the client, JSF serializes the view, optionally compresses it, and stores the resulting string in a hidden field. The name of the field, javax.faces.ViewState, is specified in the JSF 1.2 specification, so you can write code, such as the preceding JavaScript, that accesses the view state. However, earlier versions of the JSF specification did not explicitly define the name of that hidden field. As a result, if you've written code for JSF 1.1 that relies on that name, you may have to update your code for JSF 1.2.



Core JavaServerT Faces
Core JavaServer(TM) Faces (2nd Edition)
ISBN: 0131738860
EAN: 2147483647
Year: 2004
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net