Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ViewScoped beans serialized to the page when client state saving is turned on?

We have client state saving turned on and use ViewScoped backing beans. When client state saving is turned on and we are using a ViewScoped bean, is the ViewScoped bean serialzied to the page or is it say, stored in session with a token/key that is serialized to the page (so that the page can recall the bean from session if the page is posted-back to itself)

A concern here might be that, if it is serialized, then we might want to then worry about not storing large instance variables on the ViewScoped bean as it is serialized to the page and goes back/forth over the wire.

like image 834
BestPractices Avatar asked May 08 '12 18:05

BestPractices


2 Answers

When client state saving is turned on and we are using a ViewScoped bean, is the ViewScoped bean serialzied to the page or is it say, stored in session with a token/key that is serialized to the page?

Mojarra 2.x stores view scoped beans in HTTP session. There is an undocumented setting which has a default maximum of 25 view scoped beans in session. See also issue 4015. In other words, the physical view scoped bean instances are never stored in JSF view state. They are only referenced by an UUID which in turn is stored in the JSF view state. So, they are not serialized in the JSF view state, regardless of the client/server state saving method.


A concern here might be that, if it is serialized, then we might want to then worry about not storing large instance variables on the ViewScoped bean as it is serialized to the page and goes back/forth over the wire.

This is a valid concern. Even it were true, we're however talking about rather extreme cases. A collection of 100 average entities with each 10 average properties should already be no more than an additional ~5KB on the view state size. Note that you can get a lot bandwidth back by enabling gzip compression on the webserver, even up to 70% per text based resource.

If you're however dealing with large data, then the HTTP session storage size may in turn become a concern. See also JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory? Ideally, the view scoped bean should just be destroyed as soon as the page it is referencing is unloaded by a GET navigation or browser tab close. The default JSF view scoped bean doesn't do that. It's only destroyed during a postback to a different view, or when the session expires.

In case you happen to use the JSF utility library OmniFaces, since version 2.2 the @org.omnifaces.cdi.ViewScoped supports being destroyed during an unload. This should have a positive effect on HTTP session storage size.

like image 194
BalusC Avatar answered Nov 02 '22 06:11

BalusC


Please note that the answer by BalusC is not true for recent versions of JSF: for recent versions the data of the javax.faces.view.ViewScoped beans is kept on the server. See JAVASERVERFACES-3090

like image 31
Leo Mekenkamp Avatar answered Nov 02 '22 05:11

Leo Mekenkamp