Reputation: 73
How to handle user defined exceptions (Custom Exception ex.: "BusinessException") in Spring MVC 3 with custom message and view name ?
For Example :
If i throw my own exception from Service layer it should be caught and should redirect to specified view with message, the view name may be same or different.
I searched in Google, but no luck.
Thanks.
Upvotes: 4
Views: 1259
Reputation: 73
----SOLVED----
Thanks for all who have helped me to achieve solution for my problem. Finally i got solution for this.
The answer is "ExceptionHandlerExceptionResolver" class from Spring 3.1, By overriding "resolveException" method.
Thank You all.
Upvotes: 0
Reputation: 31
You have to propagate exception from service layer to controller layer there you can usedeclarative exception handling and provide exception to view mapping in spring configuration xml
Upvotes: 2
Reputation: 10997
Have you checked @ExceptionHandler
for 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 advantage of having Exception handled at Spring MVC level itself , you can populate a model and bring up a meaningful error page.
Otherwise you can configure it in web.xml as other answer suggests. But your error page will be more like a static page.
Upvotes: 6