Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is viewstate in JSF, and how is it used?

In JSF, there is a viewstate associated with each page, which is passed back and forth with submits etc.

I know that viewstate is calculated using the states of the various controls on the page, and that you can store it either client side or server side.

The question is: how is this value used? Is it used to validate the values sent at submit, to ensure that the same request is not sent twice?

Also, how is it calculated - I realise that richfaces may be calculated differently from myfaces, but an idea would be nice.

Thanks.

like image 863
Matthew Farwell Avatar asked May 26 '10 07:05

Matthew Farwell


People also ask

What is JSF ViewState?

JavaServer Faces (JSF) is a Java specification for building component-based user interfaces for web applications. There are two ViewState storage methods: server side and client side. By default, most implementations are configured to use server side storage.

What is javax faces State_saving_method?

The JSF parameter javax. faces. STATE_SAVING_METHOD identifies where the state of the view is stored between requests. By default, the state is saved in the servlet session. Set the STATE_SAVING_METHOD parameter to client in the context-param stanza of the web.


2 Answers

The question is: how is this value used? Is it used to validate the values sent at submit, to ensure that the same request is not sent twice?

The original reason why the viewstate exists is because HTTP is stateless. The state of the components across requests need to be maintained one way or the other. Either you store the state in memory on the server and bind it to the session, or serialize/deserialize it in the request/response each time.

AFAIK, the viewstate is not used to detect double submit, but it could if you attach a timestamp or something similar to it.

The viewstate can also be encrypted to make sure the client doesn't alter it.

Also, how is it calculated - I realise that richfaces may be calculated differently from myfaces, but an idea would be nice.

Each component is responsible to persist its state with saveState and restoreState (see this tutorial). So different component suites result in different view state. Similarly, different JSF implementations might result in different view state.

like image 158
ewernli Avatar answered Sep 17 '22 01:09

ewernli


If you're familiar with JavaScript, you can think of a JSF component tree a bit like a HTML DOM where the HTML page defines the initial state but you can alter it at runtime.

The view technology (usually JSP or Facelets) defines the initial state, but after that it can be manipulated programatically. For example, you could add a component or set a property. In order for this to work properly, the view state must be persisted between requests.

The view state is divided into two parts. The first defines the structure of the component tree:

UIView  - UIForm     - UICommand     - UIInput 

The second part defines the state of the components. These are separate due to components like UIData, where it is possible for children to have (for example) per row state. This is marshalled/unmarshalled via the StateHolder mechanisms.

like image 22
McDowell Avatar answered Sep 21 '22 01:09

McDowell