0

私の Java ee アプリケーションでは、ログアウト機能を実装できません。これは私がそれを実装しようとするとどうなるかです:私は私のアプリのヘッダーcss部分を持つheader.xhtmlを持っています:header.xhtml:(ログアウトのコード)

<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 が正しくレンダリングされず、上記のエラーが発生し、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>

どうすれば解決できますか?

4

2 に答える 2

1

loginBean が SessionScoped マネージド Bean であり、ログアウト メソッドがそのマネージド Bean のメソッドである場合にセッションを無効にするには:

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);
    }

}

メソッドで目的のページにリダイレクトするか、移動するページの名前を返すことができます。ビーンの方法でやったほうが安全だと思います。

ウェブページには、次のようなものが必要です。

<h:commandButton class="btn btn-info" action="#{loginBean.logout}" value="Log out" />
于 2014-05-03T22:39:58.143 に答える
0

これは、マネージド Bean でメソッドを呼び出そうとする間違った方法です。あなたはおそらく欲しい:

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

また、どのようにログインしていますか?独自のログイン メカニズムを展開している場合は、セッションを無効にしても問題ない可能性がありますが、web.xml セキュリティ制約を使用している場合は、サーブレットに Java EE 6 プログラムによるログイン API を使用する必要があります。

于 2012-06-13T02:11:40.937 に答える