拦截器简介
JavaWeb阶段学习过Filter(过滤器),过滤器是Servlet规范中的,对于所有的JavaWeb都适用。
此处的拦截器是spring webmvc独有的,只能在spring webmvc中使用。功能和过滤器雷同。
不管是过滤器还是拦截器,都是AOP编程思想的具体表现。
编写拦截器的步骤和执行流程
编写一个类,实现HandlerInterceptor接口即可,重写接口方法,实现想要的效果
package com.itheima.interceptors;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor1 implements HandlerInterceptor {
//在处理方法执行前先执行。此方法必须返回true,才会有后续的操作
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle1111.............");
return true;
}
//在处理方法执行完毕后执行。
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle1111.............");
}
//响应视图已经产生后执行(此时响应内容还未输出给浏览器)
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion1111.............");
}
}
详细执行顺序图示如下:
修改spring配置文件,配置拦截器,并指定要拦截的访问
<mvc:interceptors>
<mvc:interceptor>
<!--
绝对匹配:/test/test07
通配符匹配:/test/* 路径以/test/xxx拦截
通配符匹配:/test/** 只要以test开头的方法都会拦截
mvc:exclude-mapping:配置不需要拦截的路径(特例)
-->
<mvc:mapping path="/test/test07"/>
<bean class="com.itheima.interceptors.MyInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
编写视图页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>默认主页</title>
</head>
<body>
<a href="test/test07">拦截器</a><br/>
</body>
</html>
编写处理方法
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("test")
public class TestController {
@RequestMapping("test07")
public String test06(){
System.out.println("testHandler test06......");
return "success";
}
}
创建success.jsp响应视图
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>提示</title>
</head>
<body>
操作成功!<br/>
<%
System.out.println("success.jsp.....");
%>
</body>
</html>
运行结果:控制台打印了以下内容:
preHandle111.............
testhandler test07......
postHandle1111...........
success.jsp.....
afterCompletion1111.............
串联拦截器
多个拦截器对相同资源的访问进行拦截。执行顺序遵守的FILO(First In Last Out先入后出)。
- 再编写一个拦截器
package com.itheima.interceptors;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor2 implements HandlerInterceptor {
//在处理方法执行前先执行。此方法必须返回true,才会有后续的操作
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle2222.............");
return true;
}
//在处理方法执行完毕后执行。
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle2222.............");
}
//响应视图已经产生后执行(此时响应内容还未输出给浏览器)
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion2222.............");
}
}
- 配置拦截器:多个拦截器拦截顺序根据配置文件的先后顺序决定的
<mvc:interceptors>
<mvc:interceptor>
<!--
绝对匹配:/test/test07
通配符匹配:/test/* 路径以/test/xxx拦截
通配符匹配:/test/** 只要以test开头的方法都会拦截
mvc:exclude-mapping:配置不需要拦截的路径(特例)
-->
<mvc:mapping path="/test/test07"/>
<bean class="com.itheima.interceptors.MyInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/test/test07"/>
<bean class="com.itheima.interceptors.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
测试效果:
preHandle1111.............
preHandle2222.............
testhandler test07......
postHandle2222............
postHandle1111............
success.jsp.....
afterCompletion2222.............
afterCompletion1111.............
效果体现了拦截器的顺序特点——FILO(First In Last Out先入后出)。