Skip to main content

Handling page redirection using preRenderView

3 replies [Last post]
Joined: 2009-04-22

I'm having trouble understanding what is happening with a page redirection I am trying to perform. I'm using JSF 2.0.3-SNAPSHOT (waiting for 2.0.3 release).

I have at the top of my page the following snippet:

The code for this method is the following (the relevant part anyway):

FacesContext fc = FacesContext.getCurrentInstance();
NavigationHandler navigator = fc.getApplication().getNavigationHandler();
String page = "welcome?faces-redirect=true";
navigator.handleNavigation(fc, null, page);

When I run this code, I find that this method gets called twice (which is odd) and on the 2nd time through I get a java.lang.IllegalStateException.

However if I change the page to simply "welcome", everything works perfectly except that the URL displayed in the browser is the original page (which is what I am trying to avoid).

Am I handling the navigation correctly?
Is there something I should be doing differently? I don't fully understand why the "faces-redirect" causes an error that the other case doesn't.
Is there a better way of handling this type of case (I'm trying to protect certain pages depending on whether is logged in or not).

Any ideas would be much appreciated!



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-12

> Is there a better way of handling this type of case
> (I'm trying to protect certain pages depending on
> whether is logged in or not).

You could use a node in your web.xml for that.

Joined: 2010-02-09

I am using this technique with JSF 2.0.2 on Tomcat 6 and it is working correctly. The only difference in your code vs. mine is that my redirection URL begins with a leading slash ('/'), which I believe is appropriate. I don't know if that would make any difference for you. I have not tried this on 2.0.3 or any other web container.

Joined: 2009-04-22

The error did occur with 2.0.2 as well (it's not the reason I'm using a SNAPSHOT).

I've tried adding the "/" but it doesn't work. I've included a partial stacktrace of what os happening:

at com.sun.faces.facelets.el.TagMethodExpression.invoke(
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(
at javax.faces.event.SystemEvent.processListener(
at com.sun.faces.application.ApplicationImpl.processListeners(
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(
at com.sun.faces.application.ApplicationImpl.publishEvent(
at com.sun.faces.application.ApplicationImpl.publishEvent(
at org.jboss.weld.integration.webtier.jsf.ForwardingApplication.publishEvent(
at com.sun.faces.lifecycle.RenderResponsePhase.execute(
at com.sun.faces.lifecycle.Phase.doPhase(
at com.sun.faces.lifecycle.LifecycleImpl.render(