0

In my application I am willing to navigate to an error page whenever an exception occurred. The RuntimeException is handled and checked and is thrown again. For that I have defined a global navigation rule in Faces config:

<navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <from-outcome>error</from-outcome>
        <to-view-id>/error.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

And in my catch block I am trying to navigate as:

FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
NavigationHandler navHandler = application.getNavigationHandler();
navHandler.handleNavigation(fctx,null, "error"); 

But it is not working. I am throwing an Exception forcefully but it is not navigating to the desired error page.

Any pointer would be very helpful to me.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    This is not exactly the right way of handling exceptions in JSF. Long story short, check [this demo page](https://showcase-omnifaces.rhcloud.com/showcase/exceptionhandlers/FullAjaxExceptionHandler.xhtml). – BalusC Nov 25 '12 at 12:25

1 Answers1

1

I would like to suggest to redirect error page using ExternalContext. You have to configure your Exception in web.xml

FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext.getRequestContextPath() + "/error");
extContext.redirect(url);

web.xml

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error</location>
</error-page>

If you use Jboss-Seam, it is easy to redirect the error/warnning pages base on the exception.

pages.xml

<exception class="org.jboss.seam.security.NotLoggedInException">
    <redirect view-id="/login.xhtml">
        <message severity="warn">#{messages['org.jboss.seam.NotLoggedIn']}</message>
    </redirect>
</exception>

<exception>
    <redirect view-id="/error.xhtml">
        <message severity="error">Unexpected error, please try again</message>
    </redirect>
</exception>
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131