数据库教程:SpringMVC统一异常处理三种方法详解

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

这篇文章主要介绍了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="https://www.springframework.org/schema/beans"    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"    xmlns:context="https://www.springframework.org/schema/context"    xsi:schemalocation="      https://www.springframework.org/schema/beans      https://www.springframework.org/schema/beans/spring一beans.xsd      https://www.springframework.org/schema/context      https://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="https://www.springframework.org/schema/beans"    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"    xmlns:context="https://www.springframework.org/schema/context"    xsi:schemalocation="      https://www.springframework.org/schema/beans      https://www.springframework.org/schema/beans/spring一beans.xsd      https://www.springframework.org/schema/context      https://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="https://www.springframework.org/schema/beans"    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"    xmlns:context="https://www.springframework.org/schema/context"    xsi:schemalocation="      https://www.springframework.org/schema/beans      https://www.springframework.org/schema/beans/spring一beans.xsd      https://www.springframework.org/schema/context      https://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>

以上就是数据库技术:SpringMVC统一异常处理三种方法详解的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多数据库技术:SpringMVC统一异常处理三种方法详解,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/628964.html

(0)
上一篇 2021年5月30日
下一篇 2021年5月30日

精彩推荐