Spring3.0中对异常的处理方法一共提供了两种:
- 一种是使用HandlerExceptionResolver接口;
- 一种是在Controller类内部使用@ExceptionHandler注解。
使用第一种方式可以实现全局异常控制,并且Spring已经提供了一个默认的实现类SimpleMappingExceptionResolver;
使用第二种方式可以在Controller内部实现更个性化点异常处理方式,灵活性更高。一般来说,项目中只需要采用第一种方式就可以了,每个人都自己定义异常的展现方式,太过个性了,不统一。
从目前的调查结果看,这两种方式不能共存,不知道未来的版本是否能将他们合二为一,这样才能灵活配置。
基于HandlerExceptionResolver接口的异常处理:
使用这种方式只需要实现resolveException方法,该方法返回一个ModelAndView对象,在方法内部对异常的类型进行判断,然后常见合适的ModelAndView对象,如果该方法返回了null,则Spring会继续寻找其他的实现了HandlerExceptionResolver接口的Bean。换句话说,Spring会搜索所有注册在其环境中的实现了HandlerExceptionResolver接口的Bean,逐个执行,直到返回了一个ModelAndView对象。
示例代码:
- /**
- * 基于HandlerExceptionResolver接口的异常处理类
- * @author 2011-4-2
- */
- 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中去,让Spring管理它,你可以使用@Component标签,也可以使用<bean/>节点。为了简单的进行异常处理,Spring提供了SimpleMappingExceptionResolver类,该类实现了HandlerExceptionResolver接口,需要使用时只需要使用<bean/>节点进行声明即可,示例如下:
- <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="IOException">error/ioexp</prop>
- <prop key="java.sql.SQLException">error/sqlexp</prop>
- </props>
- </property>
- </bean>
基于@ExceptionHandler的异常处理:
该方法需要定义在Controller内部,然后创建一个方法并用@ExceptionHandler注解来修饰用来处理异常,这个方法基本和@RequestMapping修饰的方法差不多,只是可以多一个类型为Exception的参数,@ExceptionHandler中可以添加一个或多个异常的类型,如果为空的话则认为可以触发所有的异常类型错误。
示例代码:
- /**
- * 基于@ExceptionHandler异常处理
- * @author
- */
- @Controller
- public class ExceptionHandlerController {
- @ExceptionHandler(value={IOException.class,SQLException.class})
- public String exp(Exception ex,HttpServletRequest request) {
- request.setAttribute("ex", ex);
- return "/error.jsp";
- }
- }
以上配置基于Spring3.05进行设置,参考其《Reference Documentation》
相关推荐
spring mvc controller间跳转 重定向 传参
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
9 import org.springframework.web.servlet.mvc.Controller; 10 import org.springframework.web.servlet.ModelAndView; 11 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet....
The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...
学习Spring MVC,关于注解的Spring MVC,简单例子,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例
NULL 博文链接:https://cuisuqiang.iteye.com/blog/2043697
Spring mvc依赖包 Spring mvc都做了些什么 Controller为中心完成对系统流程的控制管理 从请求中搜集数据 对传入的参数进行验证 将结果返回给视图 针对不同的视图提供不同的解决方案 针对jsp视图技术提供标签库 拦截...
Spring MVC之Controller层的常用注解
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。, ...
主要给大家介绍了关于Spring MVC Controller返回值及异常的统一处理方法,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring MVC具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界主流的Web开发框架,Spring MVC已经成为当前热门的开发技能,同时也广泛用于桌面开发领域。 ...
在本系列文章的第一部分里,我们讨论了异常发生时,该返回给 REST API 调用者的异常表示(格式)的佳实践。 在本文(第二部分)中... HandlerExceptionResolvers 对于用一种统一的方法来处理异常来说非常的理想,
在Spring MVC中,DispatcherServlet作为前端控制器,负责请求的接收和响应结果的处理。处理器映射器(HandlerMapping)和处理器适配器(HandlerAdapter)是核心组件,负责找到和调用适当的处理器(Controller)。 ...
Spring MVC is a modern web application framework built upon the Spring Framework, and Spring Web Flow is a project that complements Spring MVC for building reusable web controller modules that ...
Spring MVC是Spring框架中的一部分,全称是Spring Web MVC,主要用于实现MVC设计模式的Web框架。它分离了控制器、模型对象、过滤器以及处理程序对象的角色,使得它们更容易进行定制。 Spring MVC的优点包括: 基于...
The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...