本文介绍了打开客户端状态保存功能后,ViewScoped Bean是否会序列化到页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已打开客户端状态保存功能,并使用ViewScoped支持bean.开启客户端状态保存功能并且我们使用的是ViewScoped bean时,是将ViewScoped bean序列化到页面上,还是说它与序列化到页面的令牌/密钥一起存储在会话中(以便页面可以调用会话中的bean(如果该页面回发到其自身)

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)

这里值得关注的是,如果将其序列化,那么我们可能要担心在将序列化到页面并通过导线来回移动的ViewScoped bean上不存储大型实例变量.

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.

推荐答案

Mojarra 2.x在HTTP会话中存储视图范围的bean.有一个未记录的设置,在会话中默认最多包含25个视图作用域的Bean.另请参见问题4015 .换句话说,物理视图范围的Bean实例永远不会以JSF视图状态存储.它们仅由UUID引用,而UUID又存储在JSF视图状态中.因此,无论采用哪种客户机/服务器状态保存方法,它们都不会在JSF视图状态下序列化.

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.

这是一个有效的问题.即使是事实,但我们所谈论的却是极端情况.具有10个平均属性的100个平均实体的集合在视图状态大小上应该已经不超过额外的〜5KB.请注意,通过在Web服务器上启用gzip压缩,您可以获得很多带宽,即使每个基于文本的资源最多可以压缩70%.

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.

但是,如果要处理大数据,则HTTP会话存储大小可能会成为一个问题.另请参见 JSF 2.2内存消耗:为什么Mojarra为什么将内存中最后25个视图的ViewScoped Bean保留在内存中?理想情况下,只要在GET导航或浏览器中卸载了该视图范围的Bean后,就应该立即销毁该视图范围的Bean.选项卡关闭.缺省的JSF视图作用域bean不会那样做.仅在回发到其他视图或会话过期时销毁它.

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.

如果您碰巧使用了JSF实用程序库 OmniFaces ,因为自版本2.2起, @org.omnifaces.cdi.ViewScoped 支持在卸载过程中销毁.这将对HTTP会话存储大小产生积极影响.

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.

这篇关于打开客户端状态保存功能后,ViewScoped Bean是否会序列化到页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:45