JSF 2-如何销毁会话范围的bean [英] JSF 2 - How to destroy a session-scoped bean

查看:129
本文介绍了JSF 2-如何销毁会话范围的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何销毁会话范围的bean?

How can I destroy a session-scoped bean?

此操作的目的是控制Bean的生存期,因此它仅在Web应用程序中的选项卡处于活动状态时才存在. (在Web应用程序中使用基于Ajax的标签导航)

The purpose of this would be to control the lifetime of the bean so it only lives when a tab in the web application is active. (Using Ajax Based Tab Navigation in the webapp)

有更好的方法吗? (自定义范围的豆子?)

Is there a better way to do that? (Custom Scoped Beans?)

推荐答案

会话作用域 bean是在涉及该bean的第一个HttpRequest上创建的. session无效时销毁.您也可以通过将其从HttpSessionsessionMap中移除(通过FacesContext获取)来手动销毁它.

Session scoped bean is created on first HttpRequest involving this bean. Destroyed when session is invalidated. You can also destroy it manually by removing it from the HttpSession, or sessionMap (get through FacesContext).

事实是,创建 Session作用域 bean(仅在选项卡视图期间有效)是不合逻辑的.为此,定义自定义范围的 bean会更好,但请首先考虑一下:

The fact is that it's illogical to create Session scoped bean which will live only during the tab view. For this exact purpose defining Custom scoped bean will be better, but think about it first:

  • 为什么需要这种功能?因为记忆?

这可能会非常棘手,想象一下用户只是经常在选项卡之间切换,而您又一次又一次地创建了Bean.更糟的是,如果您constructor@PostConstruct从数据库中获取数据,该怎么办?它不会真正有效.

Well it could be pretty tricky, imagine user just switching between the tabs pretty often and you are recreating the bean over and over. Even worse what if you fetch data from DB in constructor or @PostConstruct. It won't be really efficient.

我的观点是,您应该忘记它(除非您出于其他一些目的而需要它),然后选择一个 View scoped 视图Bean.访问该视图后,该bean会初始化,并且一直存在,直到您不切换到另一个视图为止-这样就不会在每个选项卡开关(仍然是同一视图)上提取数据库. IMO最好一次获取大量数据,而不是例如. 15次...如果您未获取任何数据,请务必使用查看范围.但这只是我的意见.

My opinion is that you should forget about it (unless you need it for some other purposes) and pick a View scoped bean. This bean gets initialized after accessing the view and lives till you don't switch to another view - so no DB fetching on each tab switch (still same view). IMO it's better to fetch huge data once than eg. 15 times... If you don't fetch any data, then go definitely for View scoped. But that's just my opinion as I said.

如果您负担不起查看范围,请选择自定义范围 bean,但绝对不要会话范围.

If you can't afford View scoped, go for Custom scoped bean, but definitely not Session scoped.

为您进一步阅读: JSF 2.0上的BalusC

希望对您有所帮助!

这篇关于JSF 2-如何销毁会话范围的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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