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

查看:125
本文介绍了是否可以在离开带有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 ,则可以使用其

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(

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天全站免登陆