2011-10-02 58 views
0

我正在使用JSF2.0与jsp。我试图将会话失效合并到我的项目中。我尝试使用以下代码。会话在JSF2.0无效

<h:commandButton value="Logout" action="#{bean.logout}" </h:commandButton> 

和我的bean类包含以下方法

public class Bean{ 
    public String logout(){ 
     FacesContext context = FacesContext.getCurrentInstance(); 
     HttpSession session = (HttpSession)context.getExternalContext().getSession(false); 
     session.invalidate(); 
     return "login"; 
    } 
    } 

其中串登录重定向到登录页面。

我的项目有几个页面,其中包括页眉..当我尝试上述方式... 它工作正常,当我点击退出从第一页...如果我尝试后相同到其他页面,它没有注销。任何人都可以帮助我...这是我们在这里使会话无效的方式吗?

UPDATE

我也试过在导航规则,使每个页面都可以被重定向到登录使用“*” ......但仍然问题是相同的

回答

2

尝试

return "login?faces-redirect=true" 

作为结果,因此浏览器不会对登录页面使用相同的请求,该会话仍处于活动状态。

+0

试过这个,但无法解决我的问题...你可以建议任何其他解决方案 – Mango

+0

它应该工作。您的问题可能与浏览器缓存相关。另请参阅以下大量相关问题:http://stackoverflow.com/q/7739712,http://stackoverflow.com/q/7838910,http://stackoverflow.com/q/8062267,http:// stackoverflow。 com/q/7163051,http://stackoverflow.com/q/7437475等。 – BalusC

0

你尝试这一点 -

return "/login?faces-redirect=true"; 

如果视图登录是在根目录下。

否则,如果它在一些其他的文件夹,然后如下 - 在结局的开始

//if it's in the folder - folder1 
return "/folder1/login?faces-redirect=true" 

通知/

0

使用remoteCommand标签和注销调用此remoteCommand使用JavaScript和提供managedBean对于注销的实施和implmentation是好的,你贴在这里只需要enter code here到redirct到登录页面,或者您可以再次使用JavaScript重定向登录页面。

<h:commandButton value="Logout" onclick="closeSession();" </h:commandButton> 

<p:remoteCommand name="closeSession" 
action="#{jobController.logout}"> 

public class Bean{ 
    public String logout(){ 
      FacesContext context = FacesContext.getCurrentInstance(); 
      HttpSession session = (HttpSession)context.getExternalContext().getSession(false); 
      session.invalidate(); 
      return "login?faces-redirect=true"; 
    } 
} 
+1

这究竟有何不同?你没有以任何方式解释OP的具体问题。 – BalusC