Friday, November 19, 2010

Calling response.sendRedirect creating an IllegalStateException in the JSF backing bean

This happens when you have response.sendRedirect(xxx) after the facesContext.responseComplete()
The right way to do is
 try {
         response.sendRedirect(“/ctxRoot/redirect.jsp”);
  }catch (IOException ioex) {
      ioex.printStackTrace();
 }
 FacesContext ctx = FacesContext.getCurrentInstance();
 ctx.getExternalContext().responseComplete();
 if (ctx.getViewRoot() != null) {                   
 //this step will clear any queued events
        ctx.getViewRoot().processDecodes(ctx);
 }

Blog Archive