jsf - Redirect in @PostConstruct causes IllegalStateException -


i want make redirect in @postconstruct in 4 of backing beans. i've learned follwoing question: jsf postconstruct exception handling - redirect know i'm supposed use:

    @postconstruct     public void init() {            if (shouldredirect) {           try {               facescontext.getcurrentinstance().getexternalcontext().redirect("bolagssok_company.xhtml");              return;           } catch (ioexception e) {              //do nothing           }         }         ....      } 

this works great 2 of backing beans... other two, non-redirected-xhtml file still making calls backing bean , doesn't redirect. i've confirmed (with debug) backing beans indeed calls both facescontext.getcurrentinstance().getexternalcontext().redirect("bolagssok_company.xhtml"); , return; statements.

any clues wrong?

redirecting in @postconstruct might late if response committed. i.e. when first few bytes of response been sent client. point of no return. can in case happen when backing bean referenced (and constructed) first time relatively late in view, maybe halfway or in end.

you solve in 1 of following ways:

  1. reference bean first time possible in view.

  2. use <f:event type="prerenderview"> instead of @postconstruct. invoke method right before render response starts (thus, before bit been sent response). or, when you're on jsf 2.2 already, use <f:viewaction>. additional advantage <f:viewaction> can return navigation case outcome return bolagssok_company?faces-redirect=true" without need fiddle externalcontext#redirect().

  3. increase default facelets buffer size javax.faces.facelets_buffer_size context param in web.xml size of largest html response.

see also:


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -