Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC表单提交中处理异常的模式_Java_Spring_Model View Controller - Fatal编程技术网

Java SpringMVC表单提交中处理异常的模式

Java SpringMVC表单提交中处理异常的模式,java,spring,model-view-controller,Java,Spring,Model View Controller,我正在处理一个遗留项目,将其转换为基于注释的MVC。我注意到onsubmit方法中有很多次遵循以下模式: public ModelAndView onSubmit(Command command) { try { service.doSomeBusinessLogic(command); } catch (ServiceException) { //return one type of model and view here

我正在处理一个遗留项目,将其转换为基于注释的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
}

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

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

如果异常是可恢复的,那么上面的方法就可以了——有一个特定的操作要执行,它在catch子句中


但是您应该决定哪些异常是可恢复的,哪些只会导致错误页面。

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

如果异常是可恢复的,那么上面的方法就可以了——有一个特定的操作要执行,它在catch子句中

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