【发布时间】:2015-01-04 05:29:45
【问题描述】:
版本:
Apache MyFaces 2.1.14
Rich Faces 4.3.5
问题:
当用户单击注销页面时,我想使 JSF 会话无效。
我决定使用PreRenderViewEvent 来做同样的事情。
以下是相同的设计:
在渲染注销页面时,通过PreRenderViewEvent在后端bean中调用一个监听器方法。
使 Java 方法中的 JSF 会话无效,例如:FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
我的问题是:这是使会话无效的正确方法吗?或者有没有更好的方法可用?
代码:
<!-- call this in Logout.xhtml page -->
<f:event listener="#{bean.invalidateSession}" type="preRenderView" />
编辑 1:
让我详细说明问题的背景。 我的系统中有两个 Web 应用程序。 当用户单击注销按钮时,它总是转到 webApp1。 要求是使 webApp1 中的会话无效,然后重定向到 webApp2 再次使会话无效。 在呈现 webapp1 LogOut 页面时将使用 preRenderView 事件。那里的侦听器将使会话无效并重定向到 webapp2 LogOut 页面,如下代码:
public void logOut(ComponentSystemEvent event) throws IOException{
//logoutAction param will be read from request
String redirectUrl = "/webapp2/Logout.xhtml?action="+logoutAction;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getExternalContext().redirect(redirectUrl);
}
【问题讨论】: