时间:2023-12-07来源:系统城装机大师作者:佚名
这篇文章主要介绍了SpringMVC-统一异常处理三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在 Spring MVC 应用的开发中,不管是对底层数据库操作,还是业务层或控制层操作,都会不可避免地遇到各种可预知的、不可预知的异常需要处理。
如果每个过程都单独处理异常,那么系统的代码耦合度高,工作量大且不好统一,以后维护的工作量也很大。
如果能将所有类型的异常处理从各层中解耦出来,这样既保证了相关处理过程的功能单一,又实现了异常信息的统一处理和维护。
幸运的是,Spring MVC 框架支持这样的实现。Spring MVC 统一异常处理有以下 3 种方式:
本节主要根据这 3 种处理方式讲解 Spring MVC 应用的异常统一处理。
Spring MVC使用SimpleMappingExceptionResolver类异常处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<? 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接口异常处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<? 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注解异常处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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" ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<? 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 > |
以上就是本文的全部内容,希望对大家的学习有所帮助。
2023-12-07
SQL SERVER 2008 r2 数据压缩的两种方法2023-12-07
SQL Server 2008数据库设置定期自动备份的方法2023-12-07
sqlserver2008首次登录失败问题及解决方法环境:SQLServer2008R2一台、web服务器一台 软件支持:iis、动态网站源代码 拓扑:步骤:数据库部分:1、开始-点击SQL Server Management Studio2、输入SQLServer服务器的名称或IP地...
2023-12-07
因为需要本地测试开发一个小型的cms系统,那么下面的sql server 2008 就可以安装下面的文件了,如果正式使用推荐安装企业版。下载地址:SQL Server 2008 Express 中文简体安装包x...
2023-12-07