本文介绍了是否可以在离开带有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.
    }
}

在幕后,它通过触发在窗口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的页面时调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:12