SpringMVC 学习-异常处理 SimpleMappingExceptionResolver 类
Spring3.0 对异常的处理方式总共有两种:
一种是使用 HandlerExceptionResolver 接口,并且 Spring 已经提供默认的实现类 SimpleMappingExceptionResolver。
第二种方法是在 Controller 内部实现,灵活性更高。
从目前的调查结果来看,这两种方式不能共存。我们一般在项目中使用第一种方法。
下面分别描述一下这两种使用方式:
一、基于 HandlerExceptionResolver 接口的方式
使用这种方式只需要实现 resolveException 方法,该方法返回一个 ModelAndView 对象,在方法内部对异常的类型进行判断,然后返回合适的 ModelAndView 对象,如果该方法返回了 null,则 Spring 会继续寻找其他的实现了 HandlerExceptionResolver 接口的 Bean。换句话说,Spring 会搜索所有注册在其环境中的实现了 HandlerExceptionResolver 接口的 Bean,逐个执行,直到返回了一个 ModelAndView 对象。
public class CustomExceptionHandler implements HandlerExceptionResolver { @Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object object, Exception exception) {
if(exception instanceof IOException){
return new ModelAndView("ioexp");
}else if(exception instanceof SQLException){
return new ModelAndView("sqlexp");
}
return null;
}
}
这个类必须声明到 Spring 配置文件中,或者使用 @Component 标签,让 Spring 管理它。同时 Spring 也提供默认的实现类 SimpleMappingExceptionResolver,需要使用时只需要使用注入到 Spring 配置文件进行声明即可。自定义实现类与默认的实现类,可同时使用。
示例如下:
<!-- 自定义的实现类 -->
<bean id="exceptionHandler" class="com.enh.test.CustomExceptionHandler"/>
<!-- 默认的实现类注入 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 为所有的异常定义默认的异常处理页面,exceptionMappings未定义的异常使用本默认配置 -->
<property name="defaultErrorView" value="error"></property>
<!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
<property name="exceptionAttribute" value="ex"></property>
<!--
定义需要特殊处理的异常,用类名或完全路径名作为key,异常页文件名作为值,
将不同的异常映射到不同的页面上。
-->
<property name="exceptionMappings">
<props>
<prop key="IOException">error/ioexp</prop>
<prop key="java.sql.SQLException">error/sqlexp</prop>
</props>
</property>
</bean>
一个典型的异常显示界面如下:
<html>
<head><title>Exception!</title></head>
<body>
<% Exception ex = (Exception)request.getAttribute("exception"); %>
<H2>Exception: <%= ex.getMessage();%></H2>
<P/>
<% ex.printStackTrace(new java.io.PrintWriter(out)); %>
</body>
</html>
exception 是在 SimpleMappingExceptionResolver 被存放到 request 中的,具体可以查看源代码。
另外这里配置的异常显示界面均仅包括主文件名,至于文件路径和后缀已经在 viewResolver 中指定。如果找不到页面,会根据错误提示再调整页面路径。
二、Controller 内部单独实现
该方法需要定义在 Controller 内部,然后创建一个方法并用 @ExceptionHandler 注解来修饰用来处理异常,这个方法基本和 @RequestMapping 修饰的方法差不多,只是可以多一个类型为 Exception 的参数,@ExceptionHandler 中可以添加一个或多个异常的类型,如果为空的话则认为可以触发所有的异常类型错误。
@Controller
public class ExceptionHandlerController { @ExceptionHandler(value={IOException.class,SQLException.class})
public String exp(Exception ex,HttpServletRequest request) {
request.setAttribute("ex", ex);
return "/error.jsp";
} }
三、相关问题
HandlerExceptionResolver 和 web.xml 中配置的 error-page 会有冲突吗?
web.xml 中配置 error-page 同样是配置出现错误时显示的页面:
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
如果 resolveException 返回了 ModelAndView,会优先根据返回值中的页面来显示。不过,resolveException 可以返回 null,此时则展示 web.xml 中的 error-page 的500状态码配置的页面。
API 文档中对返回值的解释:
return a corresponding ModelAndView to forward to, or null for default processing.
SpringMVC 学习-异常处理 SimpleMappingExceptionResolver 类的更多相关文章
- (转)SpringMVC学习(七)——Controller类的方法返回值
http://blog.csdn.net/yerenyuan_pku/article/details/72511844 本文所有案例代码的编写均建立在前文SpringMVC学习(六)——SpringM ...
- SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理
SpringMVC的异常处理,SimpleMappingExceptionResolver只能简单的处理异常 当发生异常的时候,根据发生的异常类型跳转到指定的页面来显示异常信息 ExceptionCo ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- 【SpringMVC学习07】SpringMVC中的统一异常处理
我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生.在开发中,不管是dao层 ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- springmvc学习笔记(简介及使用)
springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- SpringMVC 全局异常处理
在 JavaEE 项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度 ...
- 【Spring】SpringMVC之异常处理
java中的异常分为两类,一种是运行时异常,一种是非运行时异常.在JavaSE中,运行时异常都是通过try{}catch{}捕获的,这种只能捕获显示的异常,通常项目上抛出的异常都是不可预见.那么我们能 ...
随机推荐
- [转]iOS hacking resource collection
Link:http://www.securitylearn.net/tag/apple-ios-hacking-slides/ A collection of iOS research present ...
- GTK、KDE、Gnome、XWindows 图形界面
一.linux图形界面的实现只是linux下的应用程序实现图形界面(GUI)并不是linux的一部分,linux只是一个基于命令行的操作系统,linux和Xfree的关系就相当于当年的DOS和Wind ...
- Java学习笔记——Java程序运行超时后退出或进行其他操作的实现
当程序进入死循环或者由于其他原因无法自行终止的时候,就需要强制退出程序了. 对于开发软件 Eclipse ,在程序执行超时后,可以点击 Terminate 按钮强制退出. 那么,我们可不可以通过程序设 ...
- Android: Type Method 'NewStringUTF' could not be resolved
编译一个最简单的Android NDK 例子(android-ndk-r8/samples/hello-jni)出现了错误信息:Type Method 'NewStringUTF' could n ...
- HtmlParser应用
HtmlParser应用,使用Filter从爬取到的网页中获取需要的内容 { String url = "http://wenku.baidu.com/search?word=htmlpar ...
- javascript 闭包基础分享
javascript 闭包基础分享 闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感觉,事实上,闭包的概念在函数式编程语言中算不上是难以理解的知识.如果对作用域,函数为独立的对象这 ...
- launch failed.Binary not found
1.在eclipse官网中下载已经集成了CDT的eclipse.(http://www.eclipse.org/downloads/download.php?file=/technology/epp/ ...
- pipe----管道
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h&g ...
- 读excel时候出现java内存溢出
修改Eclipse,或MyEclipse的内存 例如MyEclipse 在window->preferences->myeclipse->application server-> ...
- 快速提高Android开发调试的使用技巧
留在这里备忘,同时如果对其他人有帮助,那就再好不过了. 1.过滤Android程序出现的异常和崩溃 adb logcat |grep --color=auto -E "System.err| ...