用于处理spring mvc的表单提交中的异常的模式

mog*_*lol 1 java model-view-controller spring

我正在研究遗留项目,将其转换为基于注释的MVC.我在onsubmit方法中注意到很多次,其中遵循以下模式:

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
}
Run Code Online (Sandbox Code Playgroud)

直觉上,这里的异常处理是错误的,但我不确定春天给我的替代解决方案是什么?任何想法或者这不是我想的反模式吗?

Boz*_*zho 5

不可恢复的异常的最佳做法是显示错误页面.在web.xml中配置它.Spring的默认异常处理程序将把响应状态设置为500,因此将显示500错误页面.

如果异常是可恢复的,那么上述方法就可以了 - 需要采取特定的操作,并且它将在catch子句中进行.

但是您应该确定哪些异常是可恢复的,哪些异常应该导致错误页面.