开发者

Pattern for handling exceptions in form submission of spring mvc

开发者 https://www.devze.com 2023-04-13 05:20 出处:网络
I am work开发者_如何学Cing on a legacy project, converting it to annotation based MVC.I am noticing alot of times in the onsubmit method where the following pattern is followed:

I am work开发者_如何学Cing on a legacy project, converting it to annotation based MVC. I am noticing alot of times in the onsubmit method where the following pattern is followed:

public ModelAndView onSubmit(Command command) {

    try {
         service.doSomeBusinessLogic(command);
    }
    catch (ServiceException) {
         //return one type of model and view here
    }

    //return another type of model and view here
}

Intuitively to me, this exception handling placed here is wrong, but I am unsure as to what alternative solutions spring gives me? Any ideas or is this not an anti-pattern like I am thinking?


The best practice for unrecoverable exceptions is to show an error page. Configure that in web.xml. Spring's default exception handler will take care of setting the response status to 500, so a 500 error page will be displayed.

If the exception is recoverable, then the above approach is fine - there is a specific action to take, and it goes in the catch clause.

But you should decide which exceptions are recoverable and which should just lead to an error page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号