支持 bean 范围如何工作?
我对 JSF 支持 bean 范围有一些误解。 我是 JSF 的新手,现在正在编写一个简单的项目,我的所有 bean 大多都有会话范围。 但是,如果我的网站有很多用户,这意味着我的会话将非常非常大,并且会杀死我的服务器。 有些人告诉我解决方案是使用请求范围 bean。 但是,例如,当我的页面必须经过验证并且验证失败时会显示错误消息并保存所有用户输入,在这种情况下我该怎么办? 我正在尝试使用使用 AJAX 请求的组件,并希望我的请求 bean 不会被重建,但这不起作用(我正在使用
)。
我认为我对 JSF 的理解有很大的漏洞,如果有人解释我在这种情况下必须做什么或将我链接到一些关于 bean 作用域的好文章,我将不胜感激。
I have some misunderstanding with JSF backing bean scope. I am new to JSF and now writing a simple project and all my beans mostly have session scope. But if my site will have many users that means my session will be very very big and kill my server. Some people have told me that the solution is use request scope beans. But, for example, when my page must be validated and if validation is failure show error messages and save all user input, what can I do in this situation? I am trying use component that use AJAX-request and hoped that my request bean will be not reconstructed, but this doesn't work (I am using <rich:datascroller>
).
I think I have big hole in my JSF understanding, I will be grateful if somebody explain what I must do in this situation or link me on some good article about bean scopes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
范围定义了 Bean 的生命周期。
请求作用域 bean 在一个 HTTP 请求的服务期间处于活动状态,因此在您分析用户输入和制定响应页面时它们都可用。 因此,对于简单的验证和响应,我希望请求范围的 bean 是您所需要的 - 例外的是,如果您将重定向发送回浏览器并提交新请求,那么您可能需要...
会话范围的 bean在用户会话的生命周期内生存,即。 跨越多个请求。 会话可能会持续一段时间,但最终用户会注销,或者变得静止,并且他的会话会超时。 因此,您总共拥有多少用户并不重要,重要的是同时有多少用户处于活跃状态。 为每个用户保留一些会话数据(至少像谁,也许还有他最近查看的内容)是很常见的,因此没有根本理由担心某些数据被保留。 您只需要确保保持整洁,不要将旧页面的数据保留很长时间 - 也许只是一个“当前数据”bean 或类似的东西。
Scope defines the lifetime of the beans.
Request scope beans live during the servicing of one HTTP request, so thay are available both while you analyze the user's input and formulate the response page. So for simple validation and response I'd expect request-scoped beans to be what you need - the exception being perhaps if you send a redirect back to the browser and that submits a new request, then you may need ...
Session scoped beans live for the life of the user's session, ie. across several requests. Sessions might last for some time, but eventually the user logs out, or becomes quiscent and his session gets timed-out. So it doesn't matter how many users overall you have, just how many are active at once. It's pretty common to keep some session data around for each user (like at least who is, and perhaps his recently viewed stuff) so there's no fundamental reason to be worried by some data being kept. You just need to ensure you keep it tidy, don't keep the data for old pages very very long - perhaps just a "current data" bean or some such.