在 JSF 2.3 中通知用户会话结束
Notify the user about the end of the session in JSF 2.3
JavaEE、JSF-2.3、Websocket、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 页面上放置了一个按钮,单击该按钮将调用 sessionTimeOut()
方法。单击此按钮时,事件 - "exitEvent" 按预期执行,调用 PrimeFaces 脚本 PF('dlg1').show()
,显示一个对话框。
我怀疑 websockets 甚至在调用 @Predestroy
方法之前就被杀死了。
websocket 还有另一个选项,它看起来像这样:
<h:form >
<f:websocket channel="exit" scope="session" onclose="PF('dlg1').show()"/>
</h:form>
但它仅在页面加载时起作用,并且在会话结束时再次没有反应。
两个问题:
- 如何使用 websockets 处理会话结束?
- 在极端情况下,提供替代方案。
您的技术问题是您没有在 onevent
或 onclose
属性中指定函数 reference。它是这样的:
onevent="function() { PF('dlg1').show() }"
onclose="function() { PF('dlg1').show() }"
或
onevent="functionName"
onclose="functionName"
其中 functionName
定义为实函数:
function functionName() {
PF('dlg1').show();
}
Events section of javax.faces.Push
javadoc中解释了正确的方法:
<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();
}
}
另请参阅:
- Automatically perform action in client side when the session expires
- Execute JavaScript before and after the f:ajax listener is invoked
JavaEE、JSF-2.3、Websocket、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 页面上放置了一个按钮,单击该按钮将调用 sessionTimeOut()
方法。单击此按钮时,事件 - "exitEvent" 按预期执行,调用 PrimeFaces 脚本 PF('dlg1').show()
,显示一个对话框。
我怀疑 websockets 甚至在调用 @Predestroy
方法之前就被杀死了。
websocket 还有另一个选项,它看起来像这样:
<h:form >
<f:websocket channel="exit" scope="session" onclose="PF('dlg1').show()"/>
</h:form>
但它仅在页面加载时起作用,并且在会话结束时再次没有反应。
两个问题:
- 如何使用 websockets 处理会话结束?
- 在极端情况下,提供替代方案。
您的技术问题是您没有在 onevent
或 onclose
属性中指定函数 reference。它是这样的:
onevent="function() { PF('dlg1').show() }"
onclose="function() { PF('dlg1').show() }"
或
onevent="functionName"
onclose="functionName"
其中 functionName
定义为实函数:
function functionName() {
PF('dlg1').show();
}
Events section of javax.faces.Push
javadoc中解释了正确的方法:
<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();
}
}
另请参阅:
- Automatically perform action in client side when the session expires
- Execute JavaScript before and after the f:ajax listener is invoked