Question

I'm desperatly trying to write a JSF phase listener running before RESTORE_VIEW which forwards to different pages depending on login state, permanent login state, requestes view, etc... It's a bit complicated but to ease all up, none of the following trys forwards correctly:

This one seems to work only if there is also a navigation case in faces-config as it ends up with "... no navigation case found ..."

String path = "/user/login.xhtml";
FacesContext context = event.getFacesContext(); // PhaseEvent event;

context.getApplication().getNavigationHandler().handleNavigation(context, null, path);

Same thing with the following:

context.getExternalContext().dispatch(path);

And in the last one getViewRoot() always returns null:

context.getViewRoot().setViewId(path);

So how can I forward without defining a navigation case?

It turned out that redirection works:

  String requestUrlString = ((HttpServletRequest)(FacesContext.getCurrentInstance().getExternalContext().getRequest())).getRequestURL().toString();
  URL requestUrl = new URL(requestUrlString);
  String linkUrlString = requestUrl.getProtocol() + ":" + "//"
          + requestUrl.getHost() + ":" + requestUrl.getPort()
          + context.getExternalContext().getApplicationContextPath() + "/faces" + path + ".xhtml";
  context.getExternalContext().redirect(linkUrlString);
Was it helpful?

Solution

Try this:

FacesContext ctx = FacesContext.getCurrentInstance();
ctx.getExternalContext().redirect(outcome);
ctx.responseComplete();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top