为什么JSF会在服务器上保存UI组件的状态? [英] Why JSF saves the state of UI components on server?

查看:187
本文介绍了为什么JSF会在服务器上保存UI组件的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 直到什么时候JSF在服务器端保存UI组件的状态&什么时候从服务器内存中删除了UI组件的状态信息?当应用程序中的登录用户浏览页面时,组件的状态是否会继续累积在服务器上?

  1. Until what point in time does JSF save the state of UI components on the server side & when exactly is the UI component's state information removed from the server memory? As a logged-in user on the application navigates though pages, will the state of components keep on accumulating on the server?

我不明白在服务器上保持UI组件状态有什么好处?是不是直接将经过验证/转换的数据传递给托管bean?我可以或者应该尽量避免它吗?

I don't understand what is the benefit of keeping UI components state on server !? Isn't directly passing the validated/converted data to managed beans enough? Can I or should I try to avoid it?

如果有数千个并发用户会话,那么服务器端是否会消耗太多内存?我有一个应用程序,用户可以在某些主题上发布博客。这个博客的规模非常大。如果有回复或请求查看博客,是否会将此大页面数据保存为组件状态的一部分?这会占用太多内存。这不是一个问题吗?

Doesn't that consume too much memory on the server side, if there are thousands of concurrent user sessions? I have an application where users can post blogs on certain topics. This blogs are quite large in size. When there will be post back or request for viewing the blogs, will this big page data be saved as a part of the state of components? This would eat up too much memory. Isn't this a concern ?






更新1:



现在,在使用JSF时不再需要保存状态。可以使用高性能无状态JSF实现。请参阅此博客&相关的此问题细节&讨论。此外,还有一个开放的问题,以包含在JSF规范中,提供无状态模式的选项对于JSF。
(PS考虑对 this & 这个如果这是一个有用的功能。)


Update 1:

Now, it is no longer necessary to save state while using JSF. A high performance Stateless JSF implementation is available for use. See this blog & this question for relevant details & discussion. Also, there is an open issue to include in JSF specs, an option to provide stateless mode for JSF. (P.S. Consider voting for the issues this & this if this is a useful feature for you.)


一个伟大的 Mojarra 2.1.19 的消息是无状态模式

请看这里:

http://weblogs.java.net/blog/mriem/archive/2013/02/08/jsf-going-stateless?force=255

http://java.net/jira/browse/JAVASERVERFACES-2731

http:// balusc。 blogspot.de/2013/02/stateless-jsf.html

推荐答案


为什么JSF需要在服务器端保存UI组件的状态?

因为HTTP是无状态的并且JSF是有状态的。 JSF组件树受动态(程序)更改的影响。 JSF只需要知道将表单显示给最终用户时的确切状态,以便在表单提交回原始JSF组件树时,它可以基于原始JSF组件树提供的信息成功处理整个JSF生命周期。服务器。组件树提供有关请求参数名称,必需的转换器/验证器,绑定的托管bean属性和操作方法的信息。

Because HTTP is stateless and JSF is stateful. The JSF component tree is subject to dynamic (programmatic) changes. JSF simply needs to know the exact state as it was when the form had been displayed to the enduser, so that it can successfully process the whole JSF lifecycle based on the information provided by the original JSF component tree when the form has been submitted back to the server. The component tree provides information about the request parameter names, the necessary converters/validators, the bound managed bean properties and action methods.

直到什么时候JSF在服务器端保存UI组件的状态,以及何时从服务器内存中删除UI组件的状态信息?

这两个问题似乎归结为相同。无论如何,这是特定于实现的,还取决于状态是保存在服务器还是客户端上。有点不错的实现将在它过期或队列已满时将其删除。例如,当状态保存设置为会话时,Mojarra的默认限制为15个逻辑视图。这可以通过 web.xml 中的以下上下文参数进行配置:

Those two questions seem to boil down to the same. Anyway, this is implementation specific and also dependent on whether the state is saved on server or client. A bit decent implementation will remove it when it has been expired or when the queue is full. Mojarra for example has a default limit of 15 logical views when state saving is set to session. This is configureable with the following context param in web.xml:

<context-param>
    <param-name>com.sun.faces.numberOfLogicalViews</param-name>
    <param-value>15</param-value>
</context-param>

参见 Mojarra FAQ 其他Mojarra特定参数及相关答案 com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews

See also Mojarra FAQ for other Mojarra-specific params and this related answer com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews


当应用程序中的登录用户浏览页面时,组件的状态是否会继续累积在服务器上?

从技术上讲,这取决于实施。如果您正在谈论页面到页面导航(只是GET请求),那么Mojarra将不会在会话中保存任何内容。如果它们是POST请求(带有commandlinks / buttons的表单),那么Mojarra将在会话中保存每个表单的状态,直到达到最大限制。这使得最终用户可以在同一会话中的不同浏览器选项卡中打开多个表单。

Technically, that depends on the implementation. If you're talking about page-to-page navigation (just GET requests) then Mojarra won't save anything in session. If they are however POST requests (forms with commandlinks/buttons), then Mojarra will save state of each form in session until the max limit. This enables the enduser to open multiple forms in different browser tabs in the same session.

或者,当状态保存设置为客户端时,JSF将不会存储任何内容在会议中。您可以通过 web.xml 中的以下上下文参数执行此操作:

Or, when the state saving is set to client, then JSF won't store anything in session. You can do that by the following context param in web.xml:

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

然后将序列化为隐藏输入字段中的加密字符串,名称为表格的javax.faces.ViewState

It will then be serialized to an encrypted string in a hidden input field with the name javax.faces.ViewState of the form.


我不明白在服务器端保持UI组件状态的好处是什么。是不是直接将经过验证/转换的数据传递给托管bean?可以/应该尽量避免吗?

这还不足以确保JSF的完整性和健壮性。 JSF是一个具有单一控制点的动态框架。如果没有状态管理,就可以以某种方式欺骗/破解HTTP请求(例如,操纵已禁用只读呈现属性),让JSF做出不同的 - 并且可能带来危险的事情。它甚至会容易发生CSRF攻击和网络钓鱼。

That's not enough to ensure the integrity and robustness of JSF. JSF is a dynamic framework with a single entry point of control. Without a state management, one would be able spoof/hack HTTP requests in a certain way (e.g. manipulating disabled, readonly and rendered attributes), to let JSF do different -and potentially hazardful- things. It would even be prone to CSRF attacks and phishing.


赢了'如果有数千个并发用户会话,那么在服务器端消耗太多内存?我有一个应用程序,用户可以在某些主题上发布博客。这个博客的规模非常大。当有回访或请求查看博客时,大型博客将被保存为组件状态的一部分。这会消耗太多内存。这不是一个问题吗?

内存特别便宜。只需给appserver足够的内存。或者,如果网络带宽对您来说更便宜,只需将状态保存切换到客户端即可。要找到最佳匹配,只需使用预期的最大并发用户数进行压力测试并分析您的webapp,然后为应用程序服务器提供最大测量内存的125%~150%。

Memory is particularly cheap. Just give the appserver enough memory. Or if network bandwidth is cheaper to you, just switch state saving to client side. To find the best match, just stresstest and profile your webapp with expected max amount of concurrent users and then give the appserver 125% ~ 150% of maximum measured memory.

注意JSF 2.0在状态管理方面有了很大的改进。可以保存部分状态(例如 < h:form> 将被保存而不是< html> 一直到最后)。例如Mojarra就是这么做的。具有10个输入字段(每个字段带有标签和消息)和2个按钮的平均表单不会超过1KB。在会话中有15个视图,每个会话不应超过15KB。大约1000个并发用户会话,不应超过15MB。

Note that JSF 2.0 has improved a lot in state management. It's possible to save partial state (e.g. only the <h:form> will be saved instead of the whole stuff from <html> all the way to the end). Mojarra for example does that. An average form with 10 input fields (each with a label and message) and 2 buttons would take no more than 1KB. With 15 views in session, that should be no more than 15KB per session. With ~1000 concurrent user sessions, that should be no more than 15MB.

您的关注点应该更多地关注真实对象(托管bean和/或甚至数据库实体)在会话或应用程序范围内。我已经看到很多代码和项目不必要地将整个数据库表复制到Java的内存中,其中使用了Java而不是SQL来过滤/分组/排列记录。有大约1000条记录,每个用户会话就可以轻松超过10MB

Your concern should be more focused on the real objects (managed beans and/or even DB entities) in session or application scope. I've seen lot of codes and projects which unnecessarily duplicates the entire database table into Java's memory in flavor of a session scoped bean where Java is been used instead of SQL to filter/group/arrange the records. With ~1000 records, that would easily go over 10MB per user session.

这篇关于为什么JSF会在服务器上保存UI组件的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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