views:

42

answers:

3

How can I forward to another page inside the constructor of a backing bean? I don't want to use redirect.

A: 

You need to invoke a method to do this. Constructors are not methods, and cannot return the "PAGE2" string.

Thorbjørn Ravn Andersen
+1  A: 

You can use ExternalContext#dispatch() for this.

public Bean() {
    FacesContext.getCurrentInstance().getExternalContext().dispatch("page.jsf");
}

However, there is one caveat: if the response is already committed, then it will stay at the same page and you will face IllegalStateException: Response already committed in the server logs. Whether this happens depends entirely on the moment in the JSF lifecycle you're invoking the bean's constructor and the response buffer size of the servletcontainer you're using. The same problem would however be exposed as well when you're using ExternalContext#redirect().

BalusC
A: 

Thank you very much BalusC.

Is FacesContext.getCurrentInstance().getExternalContext().dispatch("page.jsf") safe if I call it inside a getter method? The getter method will be invoked by JSF in the Render Response phase.

anonymous