10

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

4

4 回答 4

6

使用本机 JSF 或 PrimeFaces 时不会。您最好的选择是挂钩会话到期。

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. 这将@PreDestroy在离开引用视图范围 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()中已弃用)的窗口事件。

也可以看看:

于 2012-04-03T02:44:55.043 回答
3

If you use omnifaces @ViewScoped annotation in the backing bean the bean object is destroyed when you leave the view; so you can call a function when this happens using the @PreDestroy annotation in it.

Note: You must use omnifaces @ViewScoped annotation; with standard JSF @ViewScoped annotation the object isn't destroyed just by leaving the view, so pay attention to the import!

Source: http://showcase.omnifaces.org/cdi/ViewScoped

于 2017-12-22T08:05:05.240 回答
2

您的问题解决方案:- 它适用于 java 脚本

    <head>
<title>onunload test</title>
<script type="text/javascript">
window.onunload = unloadPage;

function unloadPage()
{
 alert("unload event detected!");
}
</script>
</head>

还有一些链接了解更多详细信息:- 链接

于 2012-04-04T04:48:15.390 回答
0

当您关闭页面或导航到另一个页面时,您可以调用 JSF 托管 bean 方法,方法是使用 a并在页面主体的“ onunload ”事件中<a4j:jsFunction/>调用它,如下所示。

<body onunload="leavingPage()">

<a4j:jsFunction name="leavingPage" action="#{myBean.myMethod}"/>
于 2012-04-03T08:27:43.447 回答