Spring MVC 3 User Exception Handling with User Defined View

How to handle user-defined exceptions (Custom Exception ex .: "BusinessException") in Spring MVC 3 with a custom message and view name?

Example:

If I throw my own exception from the service level, it should be caught and should be redirected to the specified view with a message, the name of the view may be the same or different.

I searched on google but no luck.

Thanks.

+4
source share
3 answers

---- ---- solvable

Thanks to everyone who helped me solve the problem. Finally, I got a solution for this.

The answer is the Spring 3.1 ExceptionHandlerExceptionResolver class, by overriding the resolveException method.

Thanks to everyone.

0
source

You tagged @ExceptionHandler

eg:

 @ExceptionHandler(MyBusinessException.class) public ModelAndView handleMyBusinessException(MyBusinessException e) { handle it or log it or redirect to error page after populating a model } 

This has the advantage that Exception is handled at the Spring MVC level, you can populate the model and display a meaningful error page.

Otherwise, you can configure it in web.xml, as another answer suggests. But your error page will look more like a static page.

+5
source

You should extend the exception from the service level to the controller level where you can use exception handling and provide an exception to display the mapping in the spring xml configuration

+2
source

All Articles