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

六月ゝ 毕业季﹏ 提交于 2020-01-01 05:04:31

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10504652/are-viewscoped-beans-serialized-to-the-page-when-client-state-saving-is-turned-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!