tags:

views:

365

answers:

2

Inside of a phase listener class that runs during the "RESTORE_VIEW" phase, I have some code like this:

public void afterPhase(PhaseEvent event) {
  FacesContext fc = event.getFacesContext();
  NavigationHandler nh = fc.getApplication().getNavigationHandler();
  nh.handleNavigation(fc, null, "/a/specific/path/to/a/resource.jspx");
}

Navigation to the new URL doesn't work here. The request made will just receive a response from the original JSPX that was navigated to.

Code like this works fine:

public void afterPhase(PhaseEvent event) {
  FacesContext fc = event.getFacesContext();
  NavigationHandler nh = fc.getApplication().getNavigationHandler();
  nh.handleNavigation(fc, null, "OUTCOME_DEFINED_IN_FACES_CONFIG_XML");
}

Also the first snippet will work with an IceFaces Faces provider, but not Sun JSF 1.2 which is what I need to use. Is there something I can do to fix the code so it is possible to forward to specific URLs?

+1  A: 

Use ExternalContext#dispatch() instead.

Or, if it is supposed to be a redirect, use ExternalContext#redirect().

That it works in IceFaces must be a bug or impl-specific "feature". This is namely not what the NavigationHandler API contract definies.

BalusC
Hmm. Using ExternalContext.dispatch() gave me: java.lang.IllegalStateException: Cannot forward after response has been committed com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:410)
Erik Hermansen
This suggests that the `afterPhase` is actually running during `RENDER_RESPONSE` instead of `RESTORE_VIEW`. Check if `getPhaseId()` really returns `PhaseId.RESTORE_VIEW`. After all, what exactly is the problem/requirement you want to solve/achieve? A `Filter` seems to be a better place to do this stuff.
BalusC
A: 

I seemed to find an answer. The code below works fine for me:

public void afterPhase(PhaseEvent event) {
  FacesContext fc = event.getFacesContext();
  NavigationHandler nh = fc.getApplication().getNavigationHandler();
  fc.getViewRoot().setViewId("/a/specific/path/to/a/resource.jspx");
}
Erik Hermansen