1. 拦截器的概述
- SpringMVC框架中的拦截器用于对处理器进行预处理和后处理的技术。
- 可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链中的拦截器会按着定义的顺序执行。
- 拦截器和过滤器的功能比较类似,有区别
- 过滤器是Servlet规范的一部分,任何框架都可以使用过滤器技术。
- 拦截器是SpringMVC框架独有的。
- 过滤器配置了/*,可以拦截任何资源。
- 拦截器只会对控制器中的方法进行拦截。
- 拦截器也是AOP思想的一种实现方式
- 想要自定义拦截器,需要实现HandlerInterceptor接口。
2. 自定义拦截器步骤
controller中的方法
* 测试拦截器
*/
@Controller
@RequestMapping("/dept")
public class DeptController {
/**
* 测试方法
* @return
*/
@RequestMapping("/findAll.do")
public String findAll(){
System.out.println("controller方法执行了...");
return "suc";
}
}
创建类,实现HandlerInterceptor接口,重写需要的方法
* 拦截器类
*/
public class MyInterceptor2 implements HandlerInterceptor{
/**
* 拦截controller中方法。放行了,执行controller中方法。
* return true 方向
* return false 拦截
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器preHandle方法执行了...");
// 手动跳转页面
// request.getRequestDispatcher("/index.jsp").forward(request,response);
// 拦截,不放行
//return false;
// 放行
return true;
}
}
在springmvc.xml中配置拦截器类
<!--配置拦截器们-->
<mvc:interceptors>
<!--配置拦截器-->
<mvc:interceptor>
<!--该拦截器拦截哪些资源-->
<mvc:mapping path="/dept/**"/>
<!--哪些资源不想拦截
<mvc:exclude-mapping path="" />
-->
<!--拦截器对象-->
<bean class="cn.tx.demo4.MyInterceptor2" />
</mvc:interceptor>
</mvc:interceptors>
3. HandlerInterceptor接口中的方法
- preHandle方法是controller方法执行前拦截的方法
- 可以使用request或者response跳转到指定的页面
- return true放行,执行下一个拦截器,如果没有拦截器,执行controller中的方法。
- return false不放行,不会执行controller中的方法。
- postHandle是controller方法执行后执行的方法,在JSP视图执行前。
- 可以使用request或者response跳转到指定的页面
- 如果指定了跳转的页面,那么controller方法跳转的页面将不会显示。
- postHandle方法是在JSP执行后执行
- request或者response不能再跳转页面了
* 拦截器类
*/
public class MyInterceptor2 implements HandlerInterceptor{
/**
* 拦截controller中方法。放行了,执行controller中方法。
* return true 方向
* return false 拦截
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器preHandle方法执行了...");
// 手动跳转页面
// request.getRequestDispatcher("/index.jsp").forward(request,response);
// 拦截,不放行
//return false;
// 放行
return true;
}
/**
* controller方法执行后,要拦截的方法
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle方法执行了...");
// 也是可以进行页面的跳转
request.getRequestDispatcher("/index.jsp").forward(request,response);
return;
}
/**
* controller跳转的jsp页面都执行完成了,最后执行该方法
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion方法执行了...");
}
}
3. 配置多个拦截器
再编写一个拦截器的类
* 拦截器类
*/
public class MyInterceptor3 implements HandlerInterceptor{
/**
* 拦截controller中方法。放行了,执行controller中方法。
* return true 方向
* return false 拦截
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor3的preHandle方法执行了...");
// 放行
return true;
}
/**
* controller方法执行后,要拦截的方法
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor3的postHandle方法执行了...");
}
/**
* controller跳转的jsp页面都执行完成了,最后执行该方法
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor3的afterCompletion方法执行了...");
}
}
配置2个拦截器
<!--配置拦截器们-->
<mvc:interceptors>
<!--配置拦截器-->
<mvc:interceptor>
<!--该拦截器拦截哪些资源-->
<mvc:mapping path="/dept/**"/>
<!--哪些资源不想拦截
<mvc:exclude-mapping path="" />
-->
<!--拦截器对象-->
<bean class="cn.tx.demo4.MyInterceptor2" />
</mvc:interceptor>
<!--配置拦截器-->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.tx.demo4.MyInterceptor3" />
</mvc:interceptor>
</mvc:interceptors>