2012-06-09 88 views
0

在我的java ee應用程序中,我無法實現註銷功能。這是當我嘗試實現它會發生什麼: 我有頭有我的應用程序的CSS部分header.xhtml: header.xhtml:(用於註銷代碼)託管bean註銷不起作用

<div class="userid-link"><img src="images/app.png" alt=""/><p><a href="#{loginBean.logoutAction()}">Logout</a></p></div> 

代碼註銷:loginBean。 java的

public String logoutAction() 
    { 
     HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
try{ 
    HttpSession session=req.getSession(); 
    session.invalidate(); 
     // req.logout(); 
     } 
catch(Exception e) 
     { 

     } 
     return"equityVolume.xhtml"; 
    } 

錯誤:

SEVERE: Error Rendering View[/ClientTemplate/userWatch.xhtml] 
javax.el.ELException: /ClientTemplate/userWatch.xhtml @44,62 value="#{watchBean.ut}": java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) 

... 
INFO: Exception when handling error trying to reset the response. 
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed 
    at... 

主頁加載正確的,但是當我嘗試登錄時,userWatch.xhtml沒有正確渲染,我也得到次e以上錯誤,CSS也不適用。

watchBean.java

public List<UserTrack> getUt() { 
     HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
    HttpSession session=req.getSession();// debugged and found that the session is null, this methos executes after login i.e. on the userWatch.xhtml that is redirected after login from homePage 
    this.uname=(String)session.getAttribute("uname"); 
     ut=getAllUserTrack(uname); 
    return ut; 
    } 

當我刪除從header.xhtml的logOutAction方法調用,然後一切工作正常,除了我得到註銷viewExpired錯誤:

<div class="userid-link"><img src="images/app.png" alt=""/><p><a href="#/homePage">Logout</a></p></div> 

我該怎麼解決它?

+0

,能不能請您使用'request.getSession(假)'和後的結果測試嗎? – higuaro

+0

嘗試它仍然我得到相同的錯誤:( – z22

+0

檢查出以下鏈接,他們描述了應用程序拋出您的'IllegalStateException'可能的解決方案: (http://stackoverflow.com/questions/8426121/cannot -create-a-session-after-the-response-has-been-committed-why) (http://stackoverflow.com/questions/5540695/pwc3999-cannot-create-a-session-after-the-響應已被提交) (http://stackoverflow.com/questions/8072311/illegalstateexception-cannot-create-a-session-after-the-response-has-been-commi) – higuaro

回答

0

這是嘗試調用託管bean中方法的錯誤方法。你可能想要:

<h:commandLink action="#{loginBean.logoutAction()}" value="Logout" /> 

另外,你是如何登錄?如果您在使用自己的登錄機制,那麼使會話無效可能沒問題,但如果您使用的是web.xml安全性約束,那麼您應該使用Java EE 6編程式登錄API來使用servlet。

public void logout() { 
    // Invalidate session of a sessionscoped managed bean 
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
    try { 
     // Redirect to page you want after logout 
     FacesContext.getCurrentInstance().getExternalContext().redirect("<INSERT HERE THE PAGE YOU WANT TO REDIRECT(just the name like 'homepage')"); 

    } catch (IOException ex) { 
     Logger.getLogger(TravelerSession.class.getName()).log(Level.SEVERE, null, ex); 
    } 

} 

您可以重定向到頁面,你想在方法或返回的名字:

1

。如果您loginBean是SessionScoped託管bean和註銷方法是託管bean的方法無效會話你想去的頁面。我認爲通過在bean的方法中做更安全。

在網頁上,你應該有這樣的事情:

<h:commandButton class="btn btn-info" action="#{loginBean.logout}" value="Log out" />