首页 > 解决方案 > 在 JSF 2.3 中通知用户会话结束

问题描述

JavaEE、JSF-2.3、W​​ebsocket、WebApplication、WildFly。
对于每个用户,都会创建一个会话,在该会话中进行操作、授权、身份验证等。15 分钟不活动后,会话自动销毁,这要归功于web.xml的设置-

<session-config>
  <session-timeout>15</session-timeout>
</session-config>

在 JSF-2.3 中可用 WebSocket,所以我决定这样做ExitBean.java -

@Inject
@Push(channel = "exit")
PushContext push;

@PreDestroy
public void sessionTimeOut() {
    push.send("exitEvent");
}

在页面上,分别是exit.xhtml -

<h:form >
  <f:websocket channel="exit" scope="session">
    <f:ajax event="exitEvent" onevent="PF('dlg1').show()"/>
  </f:websocket>
</h:form>

在会话结束时,从日志来看,该sessionTimeOut()方法有效,它仍然@PreDestroy是,但页面上没有任何响应。对于测试,我在exit.xhtml
页面 上放置了一个按钮,通过单击调用该方法的方法。单击此按钮时,事件 - “exitEvent”按预期执行,调用 PrimeFaces 脚本,该脚本显示一个对话框。 我怀疑 websocket 甚至在调用该方法之前就被杀死了。 websocket 还有另一个选项,它看起来像这样:sessionTimeOut()PF('dlg1').show()
@Predestroy

<h:form >
  <f:websocket channel="exit" scope="session" onclose="PF('dlg1').show()"/>
</h:form>

但它仅在页面加载并且对会话结束没有反应时才起作用。
两个问题:

  1. 如何使用 websockets 处理会话结束?
  2. 在极端情况下,提供替代方案。

标签: jsfwebsocketjsf-2.3

解决方案


您的技术问题是您没有在或属性中指定函数引用。它是这样的:oneventonclose

onevent="function() { PF('dlg1').show() }"
onclose="function() { PF('dlg1').show() }"

或者

onevent="functionName"
onclose="functionName"

其中functionName被定义为实函数:

function functionName() {
    PF('dlg1').show();
}

javadoc的事件部分javax.faces.Push解释了正确的方法:

<f:websocket channel="exit" scope="session" 
    onclose="function(code) { if (code == 1000) { PF('dlg1').show() }}" />

或者

<f:websocket channel="exit" scope="session"  onclose="exitListener" />
function exitListener(code) {
    if (code == 1000) {
        PF('dlg1').show();
    }
}

也可以看看:


推荐阅读