Skip to main content

Handling page redirection using preRenderView

3 replies [Last post]
hirowla
Offline
Joined: 2009-04-22
Points: 0

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!

Thanks,

Ian

Reply viewing options

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

> 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.

markroy
Offline
Joined: 2010-02-09
Points: 0

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.

hirowla
Offline
Joined: 2009-04-22
Points: 0

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:

java.lang.IllegalStateException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:108)
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:127)
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2341)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:102)
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2040)
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:1988)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:284)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:242)
at org.jboss.weld.integration.webtier.jsf.ForwardingApplication.publishEvent(ForwardingApplication.java:336)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:104)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)