jsf - 在 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
页面
上放置了一个按钮,通过单击调用该方法的方法。单击此按钮时,事件 - “exitEvent”按预期执行,调用 PrimeFaces 脚本,该脚本显示一个对话框。
我怀疑 websocket 甚至在调用该方法之前就被杀死了。
websocket 还有另一个选项,它看起来像这样:sessionTimeOut()
PF('dlg1').show()
@Predestroy
<h:form >
<f:websocket channel="exit" scope="session" onclose="PF('dlg1').show()"/>
</h:form>
但它仅在页面加载并且对会话结束没有反应时才起作用。
两个问题:
- 如何使用 websockets 处理会话结束?
- 在极端情况下,提供替代方案。
解决方案
您的技术问题是您没有在或属性中指定函数引用。它是这样的:onevent
onclose
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();
}
}
也可以看看:
推荐阅读
- android - 如何禁用博览会开发者菜单触发器(或更改触发器)?
- python - SymPy - 符号 2D 插值,如何使 scipy.interpolate.interp2d 符号化
- gradle - 在 gradde 的另一个规则中调用规则
- javascript - 加载页面时如何始终调用 onClick?错误:超过最大更新深度
- java - 填充表格后选择第一行
- c# - 如果您需要在从同一流写入数据后读取它的数据,是否需要将 Flush 调用到 FileStream?
- java - 没有 Spring Boot 的 Spring Actuator 2
- json - 如何将 TableRow 动态添加到 Table 中?
- javascript - 如何删除匿名内联