SpringMVC统一异常处理三种方法详解

所属分类: 数据库 / mssql2008 阅读数: 1206
收藏 0 赞 0 分享

这篇文章主要介绍了SpringMVC-统一异常处理三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在 Spring MVC 应用的开发中,不管是对底层数据库操作,还是业务层或控制层操作,都会不可避免地遇到各种可预知的、不可预知的异常需要处理。

如果每个过程都单独处理异常,那么系统的代码耦合度高,工作量大且不好统一,以后维护的工作量也很大。

如果能将所有类型的异常处理从各层中解耦出来,这样既保证了相关处理过程的功能单一,又实现了异常信息的统一处理和维护。

幸运的是,Spring MVC 框架支持这样的实现。Spring MVC 统一异常处理有以下 3 种方式:

  • 使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver。
  • 实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器。
  • 使用 @ExceptionHandler 注解实现异常处理

本节主要根据这 3 种处理方式讲解 Spring MVC 应用的异常统一处理。

Spring MVC使用SimpleMappingExceptionResolver类异常处理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用扫描机制扫描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置视图解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后缀 -->
    <property name="suffix" value=".jsp" />
  </bean>
  <!--SimpleMappingExceptionResolver(异常类与 View 的对应关系) -->
  <bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!-- 定义默认的异常处理页面,当该异常类型注册时使用 -->
    <property name="defaultErrorView" value="error"></property>
    <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
    <property name="exceptionAttribute" value="ex"></property>
    <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 -->
    <property name="exceptionMappings">
      <props>
        <prop key="exception.MyException">my-error</prop>
        <prop key="java.sql.SQLException">sql-error</prop>
        <!-- 在这里还可以继续扩展对不同异常类型的处理 -->
      </props>
    </property>
  </bean>
</beans>

Spring MVC使用HandlerExceptionResolver接口异常处理

package exception;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class MyExceptionHandler implements HandlerExceptionResolver {
  @Override
  public ModelAndView resolveException(HttpServletRequest arg0,
      HttpServletResponse arg1, Object arg2, Exception arg3) {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("ex", arg3);
    // 根据不同错误转向不同页面(统一处理),即异常与View的对应关系
    if (arg3 instanceof MyException) {
      return new ModelAndView("my-error", model);
    } else if (arg3 instanceof SQLException) {
      return new ModelAndView("sql-error", model);
    } else {
      return new ModelAndView("error", model);
    }
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用扫描机制扫描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置视图解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后缀 -->
    <property name="suffix" value=".jsp" />
  </bean>
  <!--托管MyExceptionHandler-->
  <bean class="exception.MyExceptionHandler"/>
</beans>

Spring MVC使用@ExceptionHandler注解异常处理

package controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import exception.MyException;
public class BaseController {
  /** 基于@ExceptionHandler异常处理 */
  @ExceptionHandler
  public String exception(HttpServletRequest request, Exception ex) {
    request.setAttribute("ex", ex);
    // 根据不同错误转向不同页面,即异常与view的对应关系
    if (ex instanceof SQLException) {
      return "sql-error";
    } else if (ex instanceof MyException) {
      return "my-error";
    } else {
      return "error";
    }
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用扫描机制扫描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置视图解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后缀 -->
    <property name="suffix" value=".jsp" />
  </bean>
</beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用sql server management studio 2008 无法查看数据库,提示 无法为该请求检索数据 错误916解决方法

使用时代互联的海外空间,sql 2008 无限空间大小,开通账户后,使用sql server management studio 2008 连接数据库,可以链接上,但是无法查看自己的数据库,点击数据库后,提示 无法为该请求检索数据 错误916
收藏 0 赞 0 分享

探讨如何配置SQL2008,让其允许C#远程外部连接的方法详解

本篇文章是对如何配置SQL2008,让其允许C#远程外部连接的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

SQL Server 2008 R2数据库镜像部署图文教程

数据库镜像是一种针对数据库高可用性的基于软件的解决方案。其维护着一个数据库的两个相同的副本,这两个副本分别放置在不同的SQL Server数据库实例中
收藏 0 赞 0 分享

sql2008 hql语句翻译过来的分页语句介绍

有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。 特别对于UNION ALL比较有用
收藏 0 赞 0 分享

sql server 2008数据库无法启动的解决办法(图文教程)

sql server 2008数据库无法启动的解决办法(图文教程),需要的朋友可以参考一下
收藏 0 赞 0 分享

安装SQL Server 2008时 总是不断要求重启电脑的解决办法

本篇文章是对安装SQL Server 2008时,总是不断要求重启电脑的解决办法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

关于SQL Server 2008 安装提示"重新启动计算机失败"的解决办法

本篇文章是对关于SQL Server 2008 安装提示"重新启动计算机失败"的解决办法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

SQL Server Native Client下载 SQL Server Native Client安装方法

在安装 SQL Server 2008 或 SQL Server 工具时,将同时安装 Microsoft SQL Server Native Client 10.0。如果计算机上还安装了 SQL Server Native Client 的 SQL Server 2005 版本,
收藏 0 赞 0 分享

sqlserver2008 拆分字符串

数据库 拆分字符串
收藏 0 赞 0 分享

SQL2008中 阻止保存要求重新创建表的更改 的解决方法

当用户在在SQL Server 2008企业管理器中更改表结构时,必须要先删除原来的表,然后重新创建新表,才能完成表的更改
收藏 0 赞 0 分享
查看更多