[英]Is there a way to call a method upon leaving a page with JSF or PrimeFaces?
是否可以在离开JSF页面后调用方法?
Not when using native JSF or PrimeFaces. 使用本地JSF或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.
}
}
If you happen to use the JSF utility library OmniFaces , then you can use its @ViewScoped
. 如果碰巧使用了JSF实用程序库OmniFaces ,则可以使用其
@ViewScoped
。 This will call the @PreDestroy
when leaving the page referencing the view scoped bean. 当离开引用视图范围的bean的页面时,这将调用
@PreDestroy
。
import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;
@Named
@ViewScoped
public class Bean implements Serializable {
@PreDestroy
public void destroy() {
// Your code here.
}
}
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()
). 在
beforeunload
,它通过在窗口beforeunload
事件期间触发navigator.sendBeacon()
工作,并回beforeunload
同步XHR(在支持navigator.sendBeacon()
现代浏览器中已弃用)。
If you use omnifaces @ViewScoped annotation in the backing bean the bean object is destroyed when you leave the view; 如果在后备bean中使用omnifaces @ViewScoped批注,则离开视图时,该bean对象将被破坏; so you can call a function when this happens using the @PreDestroy annotation in it.
因此,当发生这种情况时,您可以使用其中的@PreDestroy批注来调用该函数。
Note : You must use omnifaces @ViewScoped annotation;
注意 :必须使用omnifaces @ViewScoped批注; with standard JSF @ViewScoped annotation the object isn't destroyed just by leaving the view, so pay attention to the import!
使用标准的JSF @ViewScoped批注,仅通过离开视图就不会破坏对象,因此请注意导入!
Source: http://showcase.omnifaces.org/cdi/ViewScoped
来源: http : //showcase.omnifaces.org/cdi/ViewScoped
Your problem Solution :- it work with java script 您的问题解决方案:-它与Java脚本一起使用
<head>
<title>onunload test</title>
<script type="text/javascript">
window.onunload = unloadPage;
function unloadPage()
{
alert("unload event detected!");
}
</script>
</head>
You can call a JSF managed bean method when you are closing the page or navigating to another page by using a <a4j:jsFunction/>
and calling it at ' onunload ' event of the page body as below. 当您关闭页面或导航到另一个页面时,可以使用
<a4j:jsFunction/>
并在页面正文的' onunload '事件中调用它来调用JSF托管bean方法,如下所示。
<body onunload="leavingPage()">
<a4j:jsFunction name="leavingPage" action="#{myBean.myMethod}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.