Jsf 2 JSF p:remoteCommand和javascript事件onbeforeunload
我们有几个用户可以登录的web应用程序。单击注销按钮,必须处理某些逻辑。问题是大多数用户在关闭浏览器时没有点击注销按钮。我尝试以下操作来调用浏览器关闭时的逻辑:Jsf 2 JSF p:remoteCommand和javascript事件onbeforeunload,jsf-2,primefaces,internet-explorer-9,java-ee-6,onbeforeunload,Jsf 2,Primefaces,Internet Explorer 9,Java Ee 6,Onbeforeunload,我们有几个用户可以登录的web应用程序。单击注销按钮,必须处理某些逻辑。问题是大多数用户在关闭浏览器时没有点击注销按钮。我尝试以下操作来调用浏览器关闭时的逻辑: 将事件“onbeforeunload”添加到我的框架集中。在浏览器关闭时,将调用注销功能 在注销函数中,我使用primefaces p:remoteCommand组件调用服务器上的操作侦听器 在当前的firefox版本中,一切正常,但我在IE9上有一些问题。在IE9中关闭一个标签调用我的逻辑。关闭浏览器不起作用。调用了My JS函数,但
function automaticLogout() {
handleAutomaticLogout();
alert('BlaBla');
}
<p:remoteCommand name="handleAutomaticLogout" actionListener="#{myBean.handleAutomaticLogout}" async="false" />
函数自动关闭(){
手足自动痛风();
警觉的('BlaBla');
}
您是否特别需要客户端Javascript解决方案,或者这就是您目前所采用的方式
在服务器端,将@PreDestroy
注释放置在支持Bean方法之上会导致在Bean超出范围之前调用该方法
如果您编写的方法(session.invalidate()
)带有此批注,则当用户未单击注销就离开您的页面时,将调用该方法
支持Bean:
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean
@SessionScoped
public class PreDestroyBean {
//Called by button - log out perhaps?
public void killTheSessionDeliberately() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
}
@PreDestroy
public void revokeLicense() {
System.out.println("PreDestroy method was called.");
}
}
<h:form>
<p:commandButton id="killSession" value="Kill Session"
actionListener="#{preDestroyBean.killTheSessionDeliberately()}" update="@form" />
</h:form>
页面按钮:
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean
@SessionScoped
public class PreDestroyBean {
//Called by button - log out perhaps?
public void killTheSessionDeliberately() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
}
@PreDestroy
public void revokeLicense() {
System.out.println("PreDestroy method was called.");
}
}
<h:form>
<p:commandButton id="killSession" value="Kill Session"
actionListener="#{preDestroyBean.killTheSessionDeliberately()}" update="@form" />
</h:form>
您可以将p:remoteCommand与onunload一起使用。 例如:
<body onunload="test();">
<form id="exit">
<p:remoteCommand id="test" name="test" action="#{ContatoMB.teste}"/>
</form>
</body>
参考
它适合我。我目前的情况是,我必须在注销时释放所请求的许可证。我们的会话超时为30分钟,但必须在用户离开应用程序后直接释放许可证。我不确定,但我认为@PreDestroy解决方案只有在会话过期时才有效?是的,我明白你的意思-抱歉,@PreDestroy需要销毁会话才能启动。融合了这两种方法,通过类似PrimeFaces RemoteCommand()的方式对会话销毁方法的Javascript调用(在主体卸载时)可能允许您在用户离开()时立即使用户的会话无效,并导致许可证撤销方法触发。