views:

16

answers:

1

Hi folks,

I am trying to show an custom error message on any occurance of exception or error in my business layer. I am catching the exception in my controller and I would like to display it in my JSP.

This exception or error is not associated with any field in the screen, its a pure server exception. I am also using an Annotated Controller. I am using Prototype for making AJAX requests to my controller.

Could some one tell me how I can do this? An example snippet would be of great help.

Cheers, J

+1  A: 

In Spring you can register a HandlerExceptionResolver which will catch exceptions thrown by your Spring MVC controllers and forward them to the view layer for rendering. These are described in the Spring docs here. Start with the SimpleMappingExceptionResolver (see javadoc) which gives a simple mechanism for mapping exception types to views.

However, if the exception occurs outside if your Spring controller, for whatever reason, then you'll need a more generic fall-back solution, which involves configuring error pages in your web.xml file. This is not Spring-specific. See here for an example of how to do it.

skaffman
Hi Skaffman, I am sorry not to have mentioned the version of Spring I am using. I am using version 2.5 and I was looking at some annotation based alternatives. Thanks for the great answer.Cheers,J
Abhishek
@jadaaih: Not an option in Spring 2.5, I'm afraid. You'll need to use XML config in either Spring or web.xml
skaffman