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

前端 未结 2 1291
粉色の甜心
粉色の甜心 2021-02-15 16:07

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 serialzi

2条回答
  •  我在风中等你
    2021-02-15 16:17

    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.

提交回复
热议问题