java JSF:清除会话范围 Bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2083836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF: Clearing Session Scoped Bean
提问by user234194
I was wondering, How would I do session.remove("userBean")in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.
我想知道,session.remove("userBean")在用户关闭 JSF 1.2 上的窗口(点击浏览器的“X”)后,我将如何在 JSF 中进行操作。
I was thinking to put a confirmation pop up after user hits 'X' of browser but not quite sure how do I access or bind the Okbutton to my defined method, where I have that logic to clear the scoped bean. Or is there any better approach of doing this?
我想在用户点击浏览器的“X”后弹出一个确认但不太确定如何访问或绑定Ok按钮到我定义的方法,在那里我有清除作用域 bean 的逻辑。或者有没有更好的方法来做到这一点?
Any help would be highly appreciated.
任何帮助将不胜感激。
Thanks.
谢谢。
回答by BalusC
I was wondering, How would I do session.remove("userBean") in JSF after user closes the window (hitting 'X' of browser) on JSF 1.2.
我想知道,在用户关闭 JSF 1.2 上的窗口(点击浏览器的“X”)后,我将如何在 JSF 中执行 session.remove("userBean") 。
You don't need to worry about it. If the session times out, then the bean will surely be removed. You can if necessary control the session timeout period in <session-config>in web.xml. If you actually want to hook on the session timeout/destroy, then you can just implement HttpSessionListenerand intercept on sessionDestroyed(), or let UserBeanimplement HttpSessionBindingListenerand intercept on valueUnbound().
您无需担心。如果会话超时,那么 bean 肯定会被删除。如有必要,您可以在<session-config>in 中控制会话超时时间web.xml。如果您真的想挂钩会话超时/销毁,那么您可以只实现HttpSessionListener和拦截 on sessionDestroyed(),或者让UserBean实现HttpSessionBindingListener和拦截 on valueUnbound()。
If you really insist in listening on the browser close button, which is quite much more work after all, then all you can do is to use the Javascript's beforeunloadevent. There are no other ways. You should only keep in mind that this is an unstandard event and that this is thus not per se supported by all webbrowsers. E.g. Opera doesn't support this event. It's quite a work to get this streamlined, because this event would also be fired when you do a plain vanilla navigation or a form submit, you'll need to suppress this event on every "normal" link/button. You'll also need to fire an Ajaxical request to inform the server about the close, because you cannot change the page location or submit a hidden form anymore as the browser is going to be closed.
如果你真的坚持要监听浏览器关闭按钮,毕竟这是更多的工作,那么你所能做的就是使用 Javascript 的beforeunload事件。没有其他方法。您应该只记住这是一个不标准的事件,因此并非所有网络浏览器都支持它本身。例如,Opera 不支持此事件。简化此操作是一项艰巨的工作,因为当您执行普通导航或表单提交时,也会触发此事件,您需要在每个“正常”链接/按钮上抑制此事件。您还需要触发一个 Ajaxical 请求来通知服务器关闭,因为您不能再更改页面位置或提交隐藏表单,因为浏览器将关闭。
回答by Adeel Ansari
Why you want to remove an attribute. In fact, when the session will be invalidated all would do garbage collected anyway, since there would be no reference. You might want to use a low timeout in this case.
为什么要删除属性。事实上,当会话失效时,无论如何都会进行垃圾收集,因为没有引用。在这种情况下,您可能希望使用较低的超时时间。
In case you want to invalidate session right away. You can always employ a JavaScript function to post the request to your server, on a click of your "Ok" button. This might not be a very reliable thing to do, but wouldn't hurt as a facility. Below is an example code,
如果您想立即使会话无效。您始终可以使用 JavaScript 函数将请求发布到您的服务器,只需单击“确定”按钮即可。这可能不是一件非常可靠的事情,但作为一种设施不会受到伤害。下面是一个示例代码,
JavaScript Function
JavaScript 函数
function show_confirm()
{
var r=confirm("Are you sure you want to abort the session?");
if (r==true) {
// set an appropriate form action and
document.form.submit();
}
}
HTML
HTML
<input type="button" onclick="show_confirm()" value="Show confirm box" />
[Edited after your comments]
[在您的评论后编辑]
So, you want to bind the button to your bean method, you have to do something custom, using CSS and JavaScript. Look at this tutorial, it will provide you some insights on how you will create a custom popup. Since its just a normal <div>, you would be doing it just like any other button, no rocket science here, just put your button code inside that <div>.
因此,您想将按钮绑定到您的 bean 方法,您必须使用 CSS 和 JavaScript 进行一些自定义操作。看看本教程,它将为您提供有关如何创建自定义弹出窗口的一些见解。因为它只是一个普通的<div>,你会像任何其他按钮一样做它,这里没有火箭科学,只需将你的按钮代码放在里面<div>。
回答by Bozho
You can use MyFaces Orchestrato define your beans in the custom conversation.accessand conversation.manual. They will allow your beans to be removed more recently than if they were stored in session.
您可以使用MyFaces Orchestra在自定义conversation.access和conversation.manual. 与存储在会话中的 bean 相比,它们将允许您的 bean 更近地被删除。
- you can define a timeout for a conversation, which is different (and smaller) from the session timeout.
- if using
conversation.access, your beans will be removed immediately after they are no longer referenced by a user request - i.e. the conversation has ended. This doesn't apply to your case (closing the browser), but is still good to know.
- 您可以为对话定义一个超时,它与会话超时不同(并且更小)。
- 如果使用
conversation.access,您的 bean 将在用户请求不再引用后立即删除 - 即对话已结束。这不适用于您的情况(关闭浏览器),但还是很高兴知道。
Conversations are sequences of steps performed by the user belonging to the same logical context - i.e. the whole registration process is one conversation, although it spans multiple pages.
对话是由属于同一逻辑上下文的用户执行的一系列步骤 - 即整个注册过程是一个对话,尽管它跨越多个页面。
Generally, you don't have to worry about what remains in the session after the user closes it. It will eventually be timeouted and garbage collected. You have to worry about what's in the session while he is browsing, because it can grow very big.
通常,您不必担心用户关闭会话后还剩下什么。它最终会超时并收集垃圾。当他浏览时,您必须担心会话中的内容,因为它可能会变得非常大。
回答by medopal
In JSF, you will be making something like
在 JSF 中,您将制作类似
JSF page:
JSF页面:
<h:commandButton id="closeBtn"
type="submit" value="Close" onclick="return showConfirmClose();"
actionListener="#{backBean.cleanUpMethod}"
</h:commandButton>
Javascript:
Javascript:
//in case the user clicked No, this method should return false,
//preventing the action from being submitted, but double check all the return values first
function showConfirmClose(){
var c = confirm("Are you sure?");
return c;
}
BackBean:
背豆:
public void cleanUpMethod(ActionEvent event){
//do cleaning here
}

