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

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

问题描述

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

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 无关;可能相同,也可能不同),然后返回到第一个选项卡并在那里提交表单.您将收到 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 应用程序真的被设计为以这种方式使用(例如,邀请在多个选项卡中打开的社交/社区站点,例如讨论论坛或问答),那么您可能会考虑使用客户端状态保存而不是增加默认值.使用客户端状态保存,您将永远不会遇到此异常.另一种方法是使用 OmniFaces 组合请求范围的 bean 和请求参数,或者一个视图范围的 bean,如果需要恢复它自己的状态,它会检查(后)构造.另一种选择是使用 <f:view瞬态=真">,这样视图不再保存,但你不能再使用视图范围的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.因此,最多 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 中导航?如何使 URL 反映当前页面(而不是前一个页面).此外,缓存在动态页面上经常被禁用,所以后退按钮基本上会给你一个全新的视图.另请参阅避免使用 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_VIEWS_/code> 被引入,具有类似的目的,但具有不同的实现并且默认禁用.

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.

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

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