简体   繁体   中英

Pattern for handling exceptions in form submission of spring mvc

I am working 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM