0

Every article and JSF2 spec keeps saying that a view is restored and saved. No one says anything about how many versions of the same view is saved per user. I can see that the value of the javax.faces.ViewState hidden variable keeps changing after every post back. I also know that I can work on the same page from two different browser tabs and each tab maintains its own state.

So, the questions is, every time a postback happens, is a new version of the view saved (in addition to the copies that are already saved)?

I need to know this for two reasons. a) to estimate session size b) to script automated testing.

Thanks.

RajV
  • 6,860
  • 8
  • 44
  • 62

2 Answers2

0

So, the questions is, every time a postback happens, is a new version of the view saved

That part is true.

(in addition to the copies that are already saved)?

That part is not true. Only newly created views (from a GET request) will be added, not restored views (from a POST request).

On a postback, the view is restored, manipulated and saved, hereby basically replacing the previous version of the very same view.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BaluesC. Thanks. Can I ask for a clarification? If I do a postback in one tab and then do a GET in another, will I not see the view as saved in tab #1? – RajV Oct 11 '12 at 18:10
  • Certainly not. What you're describing is only true for session or application scope. You should not confuse the view scope with the session scope. See also http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/7031941#7031941 – BalusC Oct 11 '12 at 18:46
  • If there is only one version saved for a page per user, I am not sure how each tab can have different values in views and disabled, hidden status etc. I will keep doing more research. Thanks. – RajV Oct 11 '12 at 19:18
  • 1
    page != view. A page can have multiple views, one per browser tab/window. The very same view as was created on initial GET request is reused on postbacks. – BalusC Oct 11 '12 at 19:29
  • I peeked into the user session and how Mojarra saves state there. Indeed, a new version of the view is added after every postback *in addition* to the old versions. See my answer below for details. – RajV Oct 12 '12 at 16:10
0

So, I did more investigation about how Mojarra saves state in the server. I went through Mojarra source code, wrote a session listener and a Servlet that prints out the contents of user session. This is what I found:

Mojarra adds a Map in the session with the name "com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap". For every unique XHTML page requested from a tab, an entry is added to that Map. This entry is also a Map. View state is saved within this second map. From what I can see, a separate state is being saved for the initial GET request for the page and then for every postback. For example, if a user first views a page and then submits a form 3 times, then 4 separate states are saved for the same page. I believe, Mojarra has no choice but to save every postback to support the browser's back button.

Conceptually, the contents of the top level Map (the one saved in the session with the name "com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap") goes as follows:

/index.xhtml:Tab#1. This is a map and contains:
    - View state for initial GET
    - View state for postback #1
    - View state for postback #2

/index.xhtml:Tab#2. This is a map and contains:
    - View state for initial GET
    - View state for postback #1
    - View state for postback #2

/another.xhtml:Tab#1. This is a map and contains:
    - View state for initial GET
    - View state for postback #1
    - View state for postback #2

The implications of this for a busy site is frightening.

RajV
  • 6,860
  • 8
  • 44
  • 62