com.sun.faces.numberOfViewsInSession与com.sun.faces.numberOfLogicalViews [英] com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews

查看:79
本文介绍了com.sun.faces.numberOfViewsInSession与com.sun.faces.numberOfLogicalViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mojarra JSF 2的实现具有以下上下文参数:

Mojarra Implementation of JSF 2 has the following context params:

  • com.sun.faces.numberOfViewsInSession(默认为15)
  • com.sun.faces.numberOfLogicalViews(默认为15)
  • com.sun.faces.numberOfViewsInSession (default is 15)
  • com.sun.faces.numberOfLogicalViews (default is 15)

它们之间有什么区别?该文档没有太多谈论这些.我的应用在某些页面上无法使用ViewExpiredException,但是在将这些设置提高到更高的值后,我们不再遇到问题.

What is the difference between them? The documentation doesn't speak much about these. My app was having trouble with ViewExpiredException for some pages, but after we bumped these settings to a (much) higher value, we stopped having problems.

我的应用程序是一款财务,具有大量表单功能且支持Ajax的应用程序(某些屏幕具有50多个输入,并可以选择通过AJAX添加更多数据/输入).

My app is a financial, form-heavy, ajax-enabled app (some screens have 50+ inputs, with the option of adding alot more data/inputs via AJAX).

这种行为的原因是什么?我知道第一个参数定义了会话中保留的页面"数,这对于后退按钮可能很有用,但是触发ViewExpiredException的用例不使用后退按钮.第二个参数指的是什么?如果我停留在同一屏幕上,但继续通过AJAX添加大量数据,这是否会导致页面需要大量逻辑视图?

what can be the cause for this behaviour? I understand that the first param defines the number of "pages" that are kept in session, which may be useful for the back button, but my use cases that trigger the ViewExpiredException don't use the back button. What does the second param refer to? If I stay in the same screen but keep adding alot of data via AJAX, does this cause the need of a larger number of logical views for page?

推荐答案

首先,Mojarra实现无意间交换了这些上下文参数的含义.因此,如果您觉得描述与文字上下文参数名称所暗示的含义完全相反,那么这确实是正确的.

First of all, the Mojarra implementation unintentionally swapped the meaning of those context parameters. So if you have the impression that the description is exactly the other way round than what the literal context parameter name implies, then this is indeed true.

这基本上是基于GET请求的.每个GET请求都会在会话中创建一个新视图.

This is basically GET request based. Every GET request creates a new view in session.

要进行实验,请将其设置为3,启动新的浏览器会话并依次打开4个不同的浏览器标签(无论URL;可能相同,可能不同),然后返回第1个标签并在其中提交表单.您将得到一个ViewExpiredException,因为此视图已从LRU(最近最少使用)映射中推出,用于会话中的视图.如果您最多打开3个标签,则不会发生这种情况.

To experiment with it, set it to a value of 3, start a new browser session and open 4 different browser tabs (regardless of the URL; may be same, may be different) in sequence and then go back to the 1st tab and submit the form in there. You will get a ViewExpiredException, because this view has been pushed out from the LRU (Least Recently Used) map for views in session. This will not happen if you opened max 3 tabs.

默认值为15,这是一个罕见的现实世界问题.如果您的Web应用程序确实是设计用于这种方式的(例如,邀请在多个选项卡中打开的社交/社区网站,例如讨论论坛或Q& A),那么您可以考虑使用客户端状态保存而不是增加默认值.使用客户端状态保存,您将永远不会遇到此异常.一种替代方法是将 OmniFaces <o:enableRestorableView> 组合使用请求范围内的bean和请求参数,或者一个视图作用域的bean,它检查(后)构造是否需要恢复其自身的状态.同样,另一种选择是使用<f:view transient="true"> 使用无状态,这样,视图不再保存,但是您不能再使用视图范围的Bean.

With the default value of 15, this is a rare real world problem. If your webapp is really designed to be used this way (e.g. a social/community site which invites to being opened in multiple tabs, such as discussion forum or Q&A), then you might consider using client side state saving instead of increasing the default value. With client side state saving, you will never face this exception. An alternative would be to use OmniFaces <o:enableRestorableView> in combination a request scoped bean and request parameters, or a view scoped bean which checks in (post)construct if its own state needs to be restored. Again another alternative is to go stateless with <f:view transient="true">, this way the views are not saved anymore, but you cannot use view scoped beans anymore.

MyFaces等效项是 org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION ,默认值为20.

The MyFaces equivalent is org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION which defaults to 20.

这基本上是基于同步(非ajax!)的POST请求.每个同步POST请求都会创建一个新的逻辑视图.它们都是基于像Map<PhysicalView, Map<LogicalView, ViewState>>这样的物理视图存储的.因此,理论上最多有15个物理视图和最多15个逻辑视图,因此会话中可以有15 * 15 = 225个视图.

This is basically synchronous (non-ajax!) POST request based. Every synchronous POST request creates a new logical view. They are all stored on basis of a physical view like so Map<PhysicalView, Map<LogicalView, ViewState>>. So, with max 15 physical views and max 15 logical views, you can theoretically have 15*15 = 225 views in session.

要进行实验,请将其设置为3,打开带有同步表单的视图,提交4次,然后按4次浏览器的后退按钮,然后再次提交表单.您将得到一个ViewExpiredException,因为此视图已从LRU(最近最少使用)映射中推出以用于逻辑视图.如果您最多返回3次然后重新提交,则不会发生这种情况.

To experiment with it, set it to a value of 3, open a view with a synchronous form, submit it 4 times and then press the browser's back button 4 times and then submit the form again. You will get a ViewExpiredException, because this view has been pushed out from the LRU (Least Recently Used) map for logical views. This will not happen if you go back max 3 times and then resubmit it.

请注意,ajax提交重用相同的逻辑视图(您可以通过查看与ajax回发中返回的值完全相同的javax.faces.ViewState值来确认它).无论如何,没有浏览器的后退按钮支持.浏览器的后退"按钮只能将您带回到上一个同步请求,因此将所有这些ajax回发存储为会话中的逻辑视图毫无意义.

Note that ajax submits reuse the same logical view (you can confirm it by seeing exactly the same javax.faces.ViewState value being returned on ajax postbacks). There's no browser's back button support for it anyway. The browser's back button only brings you back to the previous synchronous request, it would therefore not make any sense to store all those ajax postbacks as logical views in session.

默认值为15,并且当前趋势是仅使用ajax表单,并且在动态页面上禁用了缓存,这在现实世界中是非常罕见的问题.设计正确的表单不应邀请您按下浏览器的后退按钮.相反,他们应该在成功提交重定向到目标视图后,在失败时仅重新显示带有验证错误的相同表单.另请参见提示在JSF Web应用程序上避免后退按钮.如果这也适合您的应用程序,则可以安全地将值设置为1.

With the default value of 15 and the current trend of ajax-only forms and disabled cache on dynamic pages, this is a very rare real world problem. Properly designed forms shouldn't invite to pressing the browser's back button. Instead, they should on successful submit redirect to the target view and on failure just redisplay the same form with validation errors. See for hints also How to navigate in JSF? How to make URL reflect current page (and not previous one). Also, cache is more than often disabled on dynamic pages, so the back button would basically give you a brand new view back. See also Avoid back button on JSF web application. If this is also for your application the case, then you can safely set the value to 1.

MyFaces最初没有与此等效的功能,因此也将其视为会话中的物理视图.在版本2.0.6中,引入了 org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION ,用途相似,但实现方式不同,默认情况下处于禁用状态.

MyFaces originally had no equivalent for this, and counted this as a physical view in session as well. In version 2.0.6, org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION was introduced, with a similar purpose, though with a different implementation and by default disabled.

  • Why JSF saves the state of UI components on server?
  • Should PARTIAL_STATE_SAVING be set to false?
  • javax.faces.application.ViewExpiredException: View could not be restored
  • What is the usefulness of statelessness in JSF?

这篇关于com.sun.faces.numberOfViewsInSession与com.sun.faces.numberOfLogicalViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆