JSF - 检测用户何时退出浏览器窗口

发布于 2024-10-14 08:26:06 字数 258 浏览 1 评论 0原文

在我的应用程序中,我根据用户正在查看的当前页面创建了一些锁定/解锁功能。用户可以查看页面并获取存储在我的数据库中的锁,当他们离开时,锁被释放并更新数据库。

我正在尝试处理用户关闭浏览器窗口而不注销的情况。他们仍将持有锁,我需要释放该锁,并且需要进行数据库调用。

有没有办法在使用 Spring 或其他东西的 JSF 应用程序中处理这个问题?

我知道 javascript 函数 onbeforeunload 但我需要调用一个 java 方法来更新数据库。

In my application I have created some locking / unlocking functionality based on the current page that the user is viewing. A user can view a page and will acquire a lock which is stored in my database, when they navigate away the lock is released and the database is updated.

I am trying to handle the situation when the user closes the browser window without logging out. They will still be holding the lock and I need that to be released and a database call needs to occur.

Is there a way to handle this in a JSF application using Spring or something else?

I know the javascript function onbeforeunload but I need to call a java method that will enable me to update the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

思慕 2024-10-21 08:26:06

unloadbeforeunload 事件不会为您提供帮助,因为规范并不能保证在这些事件中触发的任何 ajax 请求都会成功命中服务器。通常情况下,这不会。

最可靠的方法是保持服务器端会话生命周期非常短,例如 1 分钟,并在页面上引入 ajax 轮询器,该轮询器每分钟(减去几秒)在同一会话中轮询服务器,直到用户处于非活动状态超过实际最大会话超时。可以通过监听 JavaScript 键盘 (keyup) 和鼠标 (click) 事件来测试用户活动。

The unload and beforeunload events aren't going to help you here since it's not by the specification guaranteed that any ajax request which is fired within those events is ever going to successfully hit the server. More than often, this won't.

The most reliable way is to keep the server side session lifetime very short, e.g. 1 minute and introduce an ajax poller on the page which polls within the same session to the server every minute (minus a few seconds) until the user is been inactive for more than the actual maximum session timeout. The user activity can be tested by listening on JavaScript keyboard (keyup) and mouse (click) events.

奈何桥上唱咆哮 2024-10-21 08:26:06

我目前正在开发一个两个窗口应用程序,需要检测窗口何时关闭。

我的 JSF 应用程序使用 ICEfaces。它为 ViewScoped beans 提供了一个 @WindowDispose 注释。使用此注释,当窗口关闭时,将调用 ViewScoped bean 的 @PreDestroy 方法。

我在绑定到 viewScoped bean 的页面中包含了一个 h:inputHidden 标记,这在检测窗口关闭和执行服务器端代码方面起作用。

I am currently working on a two window application and needed to detect when a window is closed.

My JSF application uses ICEfaces. It provides an @WindowDisposed annotation for ViewScoped beans. With this annotation, the ViewScoped bean's @PreDestroy method will be called when the window is closed.

I have included an h:inputHidden tag in my page bound to the viewScoped bean and this works in terms of detecting the window closing and executing server side code.

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