su99-bsa
su99-bsa

Reputation: 457

Redirect in @PostConstruct causes IllegalStateException

I want to make a redirect in my @PostConstruct in 4 of my backing beans. As I've learned from the follwoing question: JSF PostConstruct Exception Handling - Redirect I know that I'm supposed to use:

    @PostConstruct
    public void init() {    
       if (shouldRedirect) {
          try { 
             FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml");
             return;
          } catch (IOException e) {
             //do nothing
          }
        }
        ....
     }

This works great for 2 of my Backing beans... but for the other two, the non-redirected-xhtml file is still making calls to the backing bean and doesn't redirect. I've confirmed (with debug) that the backing beans indeed calls both FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml"); and return; statements.

Any clues what could be wrong?

Upvotes: 12

Views: 9866

Answers (1)

BalusC
BalusC

Reputation: 1108567

Redirecting in a @PostConstruct might be too late if the response is already committed. I.e. when the first few bytes of the response are already been sent to the client. This is a point of no return. That can in your case happen when the backing bean is referenced (and thus constructed) for the first time relatively late in the view, maybe about halfway or in the end.

You could solve this in one of the following ways:

  1. Reference the bean for the first time as early as possible in the view.

  2. Use <f:event type="preRenderView"> instead of @PostConstruct. This will invoke the method right before the render response starts (thus, before any bit is been sent to the response). Or, when you're on JSF 2.2 already, use the <f:viewAction>. Additional advantage is that the <f:viewAction> can return a navigation case outcome like return bolagsSok_company?faces-redirect=true" without the need to fiddle with ExternalContext#redirect().

  3. Increase the default Facelets buffer size by javax.faces.FACELETS_BUFFER_SIZE context param in web.xml to about the size of the largest HTML response.

See also:

Upvotes: 28

Related Questions