Introduction
This is hopefully one simple example of how to make a "back" button when an exception occurs, and sends you to an exception page. The question becomes "How do I get back to the page where the exception occurred?" The navigation back to the offending page is possible, but you want to make sure that you handle the exception, or you may get into a cycle.A solution was suggested to me by my friend Markus Eisele using an ExceptionHandler in JSF. I had used exception handlers in the past, and thought that it was a simple and elegant idea. The code I am providing below DOES NOT handle the exception. This is specific to your implementation. This is just a handler that sits on top of your exception hierarchy, and provides a convenient mechanism to navigate back.
Code
The example code for the NetBeans Maven project can be downloaded from BitBucket here: exception-handler-navigation-buttonYou will need to register the factory below in the
faces-config.xml
file.
GeneralExceptionHandlerFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.bluelotussoftware.jsf.exception.handler; import javax.faces.context.ExceptionHandler; import javax.faces.context.ExceptionHandlerFactory; /** * * @author John Yeary * @version 1.0 */ public class GeneralExceptionHandlerFactory extends ExceptionHandlerFactory { private ExceptionHandlerFactory parent; public GeneralExceptionHandlerFactory( final ExceptionHandlerFactory parent) { this .parent = parent; } @Override public ExceptionHandler getExceptionHandler() { ExceptionHandler result = parent.getExceptionHandler(); result = new GeneralExceptionHandler(result); return result; } } |
GeneralExceptionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.bluelotussoftware.jsf.exception.handler; import java.util.Iterator; import java.util.logging.Logger; import javax.faces.FacesException; import javax.faces.context.ExceptionHandler; import javax.faces.context.ExceptionHandlerWrapper; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.ExceptionQueuedEvent; import javax.faces.event.ExceptionQueuedEventContext; import javax.servlet.http.HttpServletRequest; /** * * @author John Yeary * @version 1.0 */ public class GeneralExceptionHandler extends ExceptionHandlerWrapper { private ExceptionHandler parent; private static final Logger LOG = Logger.getLogger(GeneralExceptionHandler. class .getName()); public GeneralExceptionHandler( final ExceptionHandler parent) { this .parent = parent; } @Override public ExceptionHandler getWrapped() { return parent; } @Override public void handle() throws FacesException { for (Iterator<exceptionqueuedevent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext eqec = (ExceptionQueuedEventContext) event.getSource(); Throwable throwable = eqec.getException(); if (throwable instanceof FacesException) { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); HttpServletRequest request = (HttpServletRequest) ec.getRequest(); String originalRequestURI = request.getRequestURI(); String encodedURL = ec.encodeRedirectURL(originalRequestURI, null ); ec.getSessionMap().put( "com.bluelotussoftware.jsf.exception.handler.GeneralExceptionHandler.URL" , encodedURL); } } parent.handle(); } } |
Usage
Here is an example of how to use it from an exception page.1 | < h:commandButton value = "Previous Page" action = "#{indexBean.navigate()}" /> |
1 2 3 4 5 | public String navigate() throws IOException { String redirectURL = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get( "com.bluelotussoftware.jsf.exception.handler.GeneralExceptionHandler.URL" ); FacesContext.getCurrentInstance().getExternalContext().redirect(redirectURL); return null ; } |
0 comments :
Post a Comment