Redirect in @PostConstruct causes IllegalStateException

jsf, jsf-2

Solution

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:

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

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()`.

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:

- Hit a bean method and redirect on a GET request

- Is there any easy way to preprocess and redirect GET requests?

- How to navigate in JSF? How to make URL reflect current page (and not previous one)

Problem

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?

Original source

Related problems