【问题标题】:Pattern for handling exceptions in form submission of spring mvcspring mvc表单提交异常处理模式
【发布时间】:2011-10-14 13:35:43
【问题描述】:

我正在处理一个遗留项目,将其转换为基于注释的 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 给了我什么替代解决方案?有什么想法,或者这不是我想的反模式吗?

【问题讨论】:

    标签: java spring model-view-controller


    【解决方案1】:

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

    如果异常是可恢复的,那么上面的方法就可以了——有一个特定的动作要采取,它进入 catch 子句。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2016-12-30
      相关资源
      最近更新 更多