SpringMVC的拦截器
SpringMVC拦截器-拦截器的作用
Spring MVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。
将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(InterceptorChain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体实现。
自定义拦截器很简单,只有如下三步:
①创建拦截器类实现HandlerInterceptor接口
public class MyInterceptor1 implements HandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle");return true;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle");}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(" afterCompletion");}
}
public class MyInterceptor2 implements HandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle2");return true;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle2");}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(" afterCompletion2");}
}
②配置拦截器
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean class="com.controller.MyInterceptor1"/></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><bean class="com.controller.MyInterceptor2"/></mvc:interceptor></mvc:interceptors>
③测试拦截器的拦截效果
@RequestMapping(value = {"/qq2"})public ModelAndView method2(ModelAndView modelAndView){modelAndView.addObject("name","ccc");modelAndView.setViewName("success");return modelAndView;}
结论:
当拦截器的preHandle方法返回true则会执行目标资源,如果返回false则不执行目标资源
多个拦截器情况下,配置在前的先执行,配置在后的后执行
拦截器中的方法执行顺序是:preHandler-------目标资源----postHandle---- afterCompletion
SpringMVC异常处理机制
异常处理的思路
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试等手段减少运行时异常的发生。
系统的Dao、Service、Controller出现都通过throws Exception向上抛出,最后由SpringMVC前端控制器交由异常处理器进行异常处理,
异常处理两种方式
① 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="defaultErrorView" value="error"/><property name="exceptionMappings"><map><entry key="java.lang.NumberFormatException" value="error"/></map></property></bean>
@RequestMapping(value = {"/qq2"})public ModelAndView method2(ModelAndView modelAndView) throws Exception{String s="abc";System.out.println(Integer.parseInt(s));modelAndView.addObject("name","ccc");modelAndView.setViewName("success");return modelAndView;}
② 实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器
自定义异常处理步骤
①创建异常处理器类实现HandlerExceptionResolver
public class MyExceptionResolver implements HandlerExceptionResolver {public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("error");return modelAndView;}
}
②配置异常处理器
<bean class="com.controller.MyExceptionResolver"/>
③编写异常页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>error</h1>
</body>
</html>
④测试异常跳转