Results 1 to 1 of 1
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    188
    Rep Power
    7

    Default Adding global exception handling using JSF 2.x ExceptionHandler

    Hi,

    i have the following classes to handle any error could happen in JSF pages.

    Java Code:
    import java.util.Iterator;
    import javax.faces.FacesException;
    import javax.faces.application.NavigationHandler;
    import javax.faces.context.ExceptionHandler;
    import javax.faces.context.ExceptionHandlerWrapper;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ExceptionQueuedEvent;
    import javax.faces.event.ExceptionQueuedEventContext;
    
    public class CustomExceptionHandler extends ExceptionHandlerWrapper {
    
        private ExceptionHandler wrapped;
    
        public CustomExceptionHandler(ExceptionHandler wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public ExceptionHandler getWrapped() {
            return wrapped;
        }
    
        @Override
        public void handle() throws FacesException {
            Iterator i = getUnhandledExceptionQueuedEvents().iterator();
            while (i.hasNext()) {
                ExceptionQueuedEvent event = (ExceptionQueuedEvent) i.next();
                ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
                
                // get the exception from context
                Throwable t = context.getException();
                FacesContext fc = FacesContext.getCurrentInstance();
                try {
                    /* Here you can use the Throwable object in order to verify the exceptions you want to handle
                     in the application */
                    NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
                    navigationHandler.handleNavigation(fc, null, "error?faces-redirect=true");
                    fc.renderResponse();
                } finally {
                    i.remove();
                }
            }
    // Call the parent exception handlerís handle() method
            getWrapped().handle();
        }
    }
    CustomExceptionHandler Class, which handels with errors.
    If any exception is thrown, it will redirect to error.xhtml
    Java Code:
    import javax.faces.context.ExceptionHandler;
    import javax.faces.context.ExceptionHandlerFactory;
    
    public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
    
        private ExceptionHandlerFactory parent;
    
    // this injection handles jsf
        public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
            this.parent = parent;
        }
    
        @Override
        public ExceptionHandler getExceptionHandler() {
            ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
            return result;
        }
    }
    And add the following lines to your faces-config.xml:
    XML Code:
    <factory>
            <exception-handler-factory>
                com.forest.exception.CustomExceptionHandlerFactory
            </exception-handler-factory>
    </factory>
    the error.xhtml under web pages
    XML Code:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html">
        <h:head>
            <title>Error</title>
            <h:outputStylesheet library="css" name="JSF-Styles.css"  />
        </h:head>
        <h:body>
            <h2 class="errorMessage">
                An error occurs. return to <a href="index.xhtml">login</a> page.
            </h2>
        </h:body>
    </html>
    Any way when i make (on purpose) any mistake in any xhtml page (like deleting close tag ">"), but no thing happens
    just page with HTTP Status 500 - but my error.xhtml isn't displayed.

    Reference: Adding global exception handling using JSF 2.x ExceptionHandler
    Any suggestions ??
    Last edited by vector_ever; 07-31-2015 at 09:14 PM.

Similar Threads

  1. Exception Handling
    By teja in forum New To Java
    Replies: 6
    Last Post: 05-15-2013, 06:41 PM
  2. exception handling
    By Elaforestis in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 04:37 AM
  3. Exception handling
    By Kartiky14 in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 08:07 AM
  4. Exception handling
    By ExX in forum New To Java
    Replies: 6
    Last Post: 03-12-2012, 11:48 AM
  5. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •