有没有办法在离开带有 JSF 或 PrimeFaces 的页面时调用方法? [英] Is there a way to call a method upon leaving a page with JSF or PrimeFaces?

查看:18
本文介绍了有没有办法在离开带有 JSF 或 PrimeFaces 的页面时调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在离开带有 JSF 的页面时调用方法?

Is there a way to call a method upon leaving a page with JSF?

推荐答案

在使用原生 JSF 或 PrimeFaces 时不会.最好的办法是挂钩会话到期.

Not when using native JSF or PrimeFaces. Your best bet would be to hook on session expiration instead.

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;

@Named
@SessionScoped
public class Bean implements Serializable {

    @PreDestroy
    public void destroy() {
        // Your code here.
    }
}

如果您碰巧使用 JSF 实用程序库 OmniFaces,那么您可以使用它的 @ViewScoped.这将在离开引用视图范围 bean 的页面时调用 @PreDestroy.

If you happen to use the JSF utility library OmniFaces, then you can use its @ViewScoped. This will call the @PreDestroy when leaving the page referencing the view scoped bean.

import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;

@Named
@ViewScoped
public class Bean implements Serializable {

    @PreDestroy
    public void destroy() {
        // Your code here.
    }
}

在幕后,它通过触发 navigator.sendBeacon() 在窗口 beforeunload 事件期间,回退到同步 XHR(在 现代浏览器支持navigator.sendBeacon()).

Under the covers, it works by triggering a navigator.sendBeacon() during the window beforeunload event with a fallback to synchronous XHR (which is deprecated in modern browsers supporting navigator.sendBeacon()).

这篇关于有没有办法在离开带有 JSF 或 PrimeFaces 的页面时调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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