侦听 JSF 托管 Bean 中的用户会话何时结束
是否可以执行这样的操作:当用户会话启动时,我从数据库中读取某个完整属性。当用户在此会话中执行某些活动时,我会更新该变量(存储在会话中)和当会话结束时,我最终将该值存储到数据库中。
我的问题是,如果用户会话已结束且已结束,如何使用 JSF 框架进行识别?然后我应该将值存储回数据库吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了
HttpSessionListener
,您可以为此使用会话范围的托管 bean。您使用@PostConstruct
(或者只是 bean 的构造函数)和@PreDestroy
用于挂钩会话创建和销毁的注释唯一的要求是在 JSF 页面中引用该 bean 或作为
@ManagedProperty
任何请求作用域的 bean。否则它不会被创建。但就您而言,这应该没有问题,因为您显然已经在使用会话范围的托管 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 destroyThe 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 框架没有单独的会话概念;它使用 Servlet 规范的底层会话管理功能。
您必须创建一个 HttpSessionListener 来提供用于捕获会话创建和销毁事件的挂钩,您可以在其中读取值并将其存储回数据库中。
The JSF framework does not have a separate concept of a session; it uses the underlying session management features of the Servlet specification.
You would have to create a HttpSessionListener that provides hooks for you to capture the session creation and destruction events, where you can read the value and store it back into the DB.
HttpSessionListener
,或者如果您需要依赖注入来进行保存,您可以使用@PostConstruct & @PreDestroy
。请记住,当您调用invalidate()
或会话超时后,会话将被销毁,而不是在用户关闭浏览器时。无论如何,为什么您要使用会话范围,对话范围可能更适合您。HttpSessionListener
, or if you need Dependency Injection for that save, you might use@PostConstruct & @PreDestroy
. Remember that the session is destroyed when you callinvalidate()
or after session timeout, not when the user closes the browser. Why do you use Session Scope anyway, Conversation Scope might fit you better.