Error Handling Jsf 2.0
Orchestra ExtVal Portlet Bridge Test Commons Ext-Scripting Sandbox Others Project Documentation Documentation Index JSF Intro Quick Start Getting Started FAQ Confluence Wiki Public Wiki Compatibility Continuous Integration Issue Tracking Mailing Lists Project License Project Summary Project Team Source Repository About Foundation ASF Sponsorship Thanks Security License Home»Wiki»MyFaces Core»MyFaces Core User Guide»JSF and MyFaces Howtos»Managing Errors - Infos - Warnings Handling Server Errors Error handling for MyFaces Core 2.0 and later versions Since JSF 2.0, it is possible to provide a custom javax.faces.context.ExceptionHandler or javax.faces.context.ExceptionHandlerWrapper implementation to deal with exceptions. To do that, just create your custom class, an factory that wrap/override it and add the following into your faces-config.xml: faces-config.xml
This Site Careers Other all forums Forum: JSF How to handle exception in JSF2.0? Amrit pandey Greenhorn Posts: 19 posted 6 years ago Hello to all, I am try to use new feature of exception handling in JSF2.0. Requirement is https://myfaces.apache.org/wiki/core/user-guide/jsf-and-myfaces-howtos/managing-errors---infos---warnings/handling-server-errors.html to show the stack trace on the error pageusing JSF2.0 exception Handling feature, if any unhandled exception occur . Problem is that, when exception is occurring , error page is displayed but it is not displaying https://coderanch.com/t/502427/JSF/java/handle-exception-JSF the error info that I am trying to display. I think I am not able to use the proper EL to retrieve the value. Please suggest..... Below are the codes: faces-config.xml
April 5, 2012April 6, 2012 470 Words https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/ Adding global exception handling using JSF 2.x ExceptionHandler This a great feature of JSF 2.x: A generic API to manipulate application http://jugojava.blogspot.com/2010/09/jsf-2-exception-handling.html exception in a global manner. In order to implement this you must implement (extend) two different classes: ExceptionHandlerWrapper - Provides a simple error handling implementation of ExceptionHandler that can be subclassed by developers wishing to provide specialized behavior to an existing ExceptionHandler instance. The default implementation of all methods is to call through to the wrapped ExceptionHandler instance. ExceptionHandlerFactory - A factory object that creates (if needed) error handling jsf and returns a new ExceptionHandler instance. On Duke's Forest this is the implementation: CustomExceptionHandlerFactory.java: package com.forest.exception; public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory { private ExceptionHandlerFactory parent; // this injection handles jsf public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) { this.parent = parent; } @Override public ExceptionHandler getExceptionHandler() { ExceptionHandler handler = new CustomExceptionHandler(parent.getExceptionHandler()); return handler; } } CustomExceptionHandlerFactory.java: package com.forest.exception; public class CustomExceptionHandler extends ExceptionHandlerWrapper { private static final Logger log = Logger.getLogger(CustomExceptionHandler.class.getCanonicalName()); private ExceptionHandler wrapped; CustomExceptionHandler(ExceptionHandler exception) { this.wrapped = exception; } @Override public ExceptionHandler getWrapped() { return wrapped; } @Override public void handle() throws FacesException { final Iterator
are thrown during the Faces lifecycle. To avoid silent exceptions that are not catched in application implement your ExceptionHandler and do what ever you want with exception. 1. Create your implementation of ExceptionHandler: public class MyExceptionHandler extends ExceptionHandlerWrapper { private static Log log = LogFactory.getLog(MyExceptionHandler.class); private ExceptionHandler wrapped; public MyExceptionHandler(ExceptionHandler wrapped) { this.wrapped = wrapped; } @Override public ExceptionHandler getWrapped() { return wrapped; } @Override public void handle() throws FacesException { //Iterate over all unhandeled exceptions Iterator i = getUnhandledExceptionQueuedEvents().iterator(); while (i.hasNext()) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource(); //obtain throwable object Throwable t = context.getException(); //here you do what ever you want with exception try{ //log error log.error("Serious error happened!", t); //redirect to error view etc.... }finally{ //after exception is handeled, remove it from queue i.remove(); } } //let the parent handle the rest getWrapped().handle(); } } 2. Create your ExceptionHandlerFactory and call your ExceptionHandler: public class MyExceptionHandlerFactory extends ExceptionHandlerFactory { private ExceptionHandlerFactory parent; // this injection handles jsf public MyExceptionHandlerFactory(ExceptionHandlerFactory parent) { this.parent = parent; } //create your own ExceptionHandler @Override public ExceptionHandler getExceptionHandler() { ExceptionHandler result = new MyExceptionHandler(parent.getExceptionHandler()); return result; } } 3. Register your exception handler factory in faces-config.xml: