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:
Code Block |
---|
|
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<!-- ... -->
<factory>
<!-- ... -->
<exception-handler-factory>org.apache.myfaces.context.ExceptionHandlerFactoryImpl</exception-handler-factory>
<!-- ... -->
</factory>
<!-- ... -->
</faces-config>
|
This is an example of an ExceptionHandlerFactory, from myfaces code:
Code Block |
---|
title | ExceptionHandlerFactoryImpl.java |
---|
|
public class ExceptionHandlerFactoryImpl extends ExceptionHandlerFactory
{
@Override
public ExceptionHandler getExceptionHandler()
{
return new SwitchAjaxExceptionHandlerWrapperImpl(
new MyFacesExceptionHandlerWrapperImpl(new ExceptionHandlerImpl()) ,
new AjaxExceptionHandlerImpl());
}
}
|
If you need a wrapper:
Code Block |
---|
title | ExceptionHandlerFactoryImpl.java |
---|
|
public class ExceptionHandlerFactoryImpl extends ExceptionHandlerFactory
{
@Override
public ExceptionHandler getExceptionHandler()
{
return new CustomExceptionHandlerWrapper(getWrapped().getExceptionHandler());
}
}
|
The most important method to override is ExceptionHandler.handle()
.
Code Block |
---|
title | MyFacesExceptionHandlerWrapperImpl.java |
---|
|
public class MyFacesExceptionHandlerWrapperImpl extends ExceptionHandlerWrapper
{
//...
public void handle() throws FacesException
{
//... some custom code goes here ...
}
}
|
Take a look at MyFaces Core source code, to know in detail how ExceptionHandler implementations worksTODO....
Error handling for MyFaces Core 1.2 and earlier versions
...