2010-11-20 51 views
0

我有一個PhaseListener監聽phaseId RENDER_RESPONSE。這個faceListener調用這個方法:IllegalStateException在重定向

public void doLogin(ServletRequest request) throws IOException { 
     FacesContext fc = FacesContext.getCurrentInstance(); 
     HttpServletRequest req = (HttpServletRequest) request; 
     String code = req.getParameter("code"); 
     if (StringUtil.isNotBlankString(code)) { 
      String authURL = Facebook.getAuthURL(code); 
      URL url = new URL(authURL); 
      try { 
       .... 
       if (accessToken != null && expires != null) { 
        boolean isLoginOk = service.authFacebookLogin(accessToken); 
        if (isLoginOk) { 
         fc.getApplication().getNavigationHandler().handleNavigation(fc, "/welcome.xhtml", "logged-in"); 
        } 
       } else { 
        throw new RuntimeException("Access token and expires not found"); 
       } 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } catch (FacebookException e) { 
       Logger.getLogger(FBOauth.class.getName()).log(Level.SEVERE, "Facebook error", e); 
      } 
     } 
    } 

    private String readURL(URL url) throws IOException { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     InputStream is = url.openStream(); 
     int r; 
     while ((r = is.read()) != -1) { 
      baos.write(r); 
     } 
     return new String(baos.toByteArray()); 
    } 

當它重定向,我得到以下異常,我真的找不到任何解決方案。從我所瞭解的情況來看,這是因爲響應已經被解決了,但爲什麼它已經被解決了?

java.lang.IllegalStateException at 
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:522) 
       at com.sun.faces.context.ExternalContextImpl.redirect(ExternalContextImpl.java:572) 
       at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:182) 
       at wmc.web.facebook.FBOauth.doLogin(FBOauth.java:57) 
       at wmc.web.listeners.FacebookSignInListener.afterPhase(FacebookSignInListener.java:56) 
       at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189) 
       at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107) 
       at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) 

通過我真的很感激所有幫助我在這裏:)獲得方式

回答

0

您的phaselistener顯然是RENDER_RESPONSEafterPhase掛鉤。現在要改變反應已經太遲了。答覆已經發送給客戶。寧可掛鉤`RENDER_RESPONSE的beforePhase()

+0

我已經做到了,但現在我回到昨天的問題:http://stackoverflow.com/questions/4225014/managed-bean-is-sometimes-null-and-sometimes-not這是更新爲當前版本 – AnAmuser 2010-11-20 11:54:36

0

我的猜測是,在執行之前sendRedirect()你的代碼的某些部分已經流文本到servlet repsonse,也許是發送給所有響應的一些通用信息?