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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With