侦听 JSF 托管 Bean 中的用户会话何时结束

发布于 2024-11-14 04:26:08 字数 156 浏览 8 评论 0原文

是否可以执行这样的操作:当用户会话启动时,我从数据库中读取某个完整属性。当用户在此会话中执行某些活动时,我会更新该变量(存储在会话中)和当会话结束时,我最终将该值存储到数据库中。

我的问题是,如果用户会话已结束且已结束,如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小耗子 2024-11-21 04:26:08

除了 HttpSessionListener,您可以为此使用会话范围的托管 bean。您使用 @PostConstruct (或者只是 bean 的构造函数)和 @PreDestroy 用于挂钩会话创建和销毁的注释

@ManagedBean
@SessionScoped
public class SessionManager {

    @PostConstruct
    public void sessionInitialized() {
        // ...
    }

    @PreDestroy
    public void sessionDestroyed() {
        // ...
    }

}

唯一的要求是在 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 destroy

@ManagedBean
@SessionScoped
public class SessionManager {

    @PostConstruct
    public void sessionInitialized() {
        // ...
    }

    @PreDestroy
    public void sessionDestroyed() {
        // ...
    }

}

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.

╄→承喏 2024-11-21 04:26:08

我的问题是如何识别使用
如果用户会话是 JSF 框架
已结束&然后我应该存储
值返回数据库?

JSF 框架没有单独的会话概念;它使用 Servlet 规范的底层会话管理功能。

您必须创建一个 HttpSessionListener 来提供用于捕获会话创建和销毁事件的挂钩,您可以在其中读取值并将其存储回数据库中。

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?

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.

活泼老夫 2024-11-21 04:26:08

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 call invalidate() or after session timeout, not when the user closes the browser. Why do you use Session Scope anyway, Conversation Scope might fit you better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文