问题描述
是否可以执行以下操作:当用户会话启动时,我从数据库中读取了某个不可分割的属性.当用户在此会话中执行某些活动时,我会更新该变量(存储在会话中)&当会话结束时,最后我将该值存储到数据库中.
Is it possible to do something like this: When a user session starts I read a certain integral attribute from the database. As the user performs certain activities in this session, I update that variable(stored in session) & when the session ends, then I finally store that value to the DB.
我的问题是,如果用户会话已结束,如何使用JSF框架进行标识?然后,我应该将值存储回数据库吗?
My question is how do I identify using the JSF framework if the user session has ended & I should then store the value back to DB?
推荐答案
除了 HttpSessionListener
,您可以为此使用会话范围的托管Bean.您使用 @PostConstruct
(或仅使用bean的构造函数) )和 @PreDestroy
注释以挂接会话创建并摧毁
Apart from the HttpSessionListener
, you can use a session scoped managed bean for this. You use @PostConstruct
(or just the bean's constructor) and @PreDestroy
annotations to hook on session creation and destroy
@ManagedBean
@SessionScoped
public class SessionManager {
@PostConstruct
public void sessionInitialized() {
// ...
}
@PreDestroy
public void sessionDestroyed() {
// ...
}
}
唯一的要求是在JSF页面中或任何请求范围的bean的@ManagedProperty
中都引用此bean.否则将无法创建.但是在您的情况下,这应该没问题,因为您显然已经在使用会话范围的托管Bean,只需添加@PreDestroy
方法就足够了.
The only requirement is that this bean is referenced in a JSF page or as @ManagedProperty
of any request scoped bean. Otherwise it won't get created. But in your case this should be no problem as you're apparently already using a session scoped managed bean, just adding a @PreDestroy
method ought to be sufficient.
这篇关于在JSF托管Bean中侦听用户会话何时结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!