8

How to do JSF internal page forward programatically in managed bean, on some condition (like whenever an exception occurs)? I do not want to change the URL while forwarding to other page.

Right now I redirect to another page programmatically using this, but this changes the URL.

FacesContext.getCurrentInstance().getExternalContext().redirect();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

2 Answers2

7

Try this:

public void forward(){
    String uri = "destination.xhtml";
    FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);
}
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Thanks,.. this works! Is this standard way to do page forward programmatically or is it a workaround ? – Rajat Gupta Apr 24 '14 at 04:57
  • 1
    @user01: this is the standard way at the moment :) – Mr.J4mes Apr 24 '14 at 06:11
  • 3
    this is Servlet redirection, to use JSF navigation handler use: FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "YOUR_NAVIGATION_CASE_DEFINED_IN_FACES_CONFIG"); – Cosmin Cosmin Nov 20 '14 at 14:14
1

You can do using

   FacesContext.getCurrentInstance().getViewRoot().setViewId("your target view id");
   FacesContext.getCurrentInstance().renderResponse();   

or you can use

   FacesContext.getCurrentInstance().responseComplete();  

Hope this helps.

Srikanth Ganji
  • 1,127
  • 1
  • 13
  • 29