Neither option is ideal for a Spring developer, elements tend to be too simplistic and it is hard to use Spring concepts, such as dependency injection, with custom s. Luckily both JSF and Spring are very extensible frameworks so a project that I have been working on to integrate the technologies can offer some compelling alternatives.
The first option available allows s to be registered as Spring beans. Rather than use the existing class a new interface is available. The interface is pretty straight forward, it defines a single method that should return if the exception has been handled. The interface uses a generic to limit the types of exception considered.
public interface ExceptionHandler<E extends Throwable> {
boolean handle(E exception, ExceptionQueuedEvent event) throws Exception;
}All relevant beans beans that implement the interface will be consulted when an exception occurs from JSF. The first handler to return will ‘win’ and subsequent handlers will not be called. You can use the interface or the annotation if you need to sort your handlers. Of course, now that exception handlers are regular Spring beans, you can use all the standard Spring functionality such a dependency injection and AOP.
Now that we have basic exception handler hooks we can go on to offer a couple of useful implementations:
Sometimes the best way to handle certain exceptions is to simply show a message and remain on the current screen. For example, suppose a service throws when search queries are too broad. A simple message telling the user to ‘try again with more precise terms’ might be the only exception handling required. The class builds on previous work that maps Objects to messages. Include an entry in your Spring with the fully qualified name of the as the key and a will be shown if the exception is thrown.
com.mycorp.search.TooManyResultsException=Too many results found, please try again with more precise search terms
You can easily map any number of exceptions to messages, you can even refer to properties of the exception using ‘‘ placeholders in your message string. Messages can be displayed on the screen using standard JSF techniques (usually a component).
Support for quickly mapping exceptions to messages is nice but it won’t be enough for a lot of applications and writing beans can quickly become tiresome. The final optional available is . The provides a bridge between JSF and Spring MVC that allows you to use annotations in your s as you would with any other Spring MVC application. Methods annotated with are really versatile and can have very flexible signatures; you can deal with exceptions directly or return a view that should be rendered:
@ExceptionHandler
public String handle(ExampleException e) {
return 'redirect:errorpage';
}Using annotations with Spring MVC is a very natural fit and there have been numerous articles written on the subject. Hopefully existing JSF developers will find the Spring MVC programming style an attractive alternative to standard JSF.
Please take a look at the other articles in this series and if you want to examine the exception handing code the ‘org.springframework.springfaces.exceptionhandler’ and ‘org.springframework.springfaces.mvc.exceptionhandler’ packages are a good place to start.
Reference: Integrating Spring & JavaServer Faces : Exception Handling from our JCG partner Phillip Webb at the Phil Webb’s Blog blog.
Thank you!
We will contact you soon.
Phillip WebbJuly 3rd, 2012Last Updated: October 22nd, 2012

This site uses Akismet to reduce spam. Learn how your comment data is processed.
Very good article but as usual Spring is really more understandable with an example or sample, it would have been really great if you could integrate you exeception handling with the current sample spring web flow reference project like the booking hotel application because right now this issue is not covered by the documentation and not integrate inside the reference sample.
One more time thanks for this ticket
I can’t find any useful online reference to either org.springframework.springfaces.exceptionhandler.ObjectMessageExceptionHandler
or simply ObjectMessageExceptionHandler.
Does this really exist?
I’m developing a Spring 3.1 MVC + JSF2 web app, and need a good, simple, reliable way to display something like FacesMessage. So far, I’ve found nothing like this in Spring. What a PITA.
Thanks
@JMS it is part of an as yet unreleased project. You can build from source or copy the code if it is useful:
https://github.com/philwebb/springfaces/blob/master/springfaces/src/main/java/org/springframework/springfaces/exceptionhandler/ObjectMessageExceptionHandler.java