2011-10-02 53 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