【Spring学习笔记-MVC-17】Spring MVC之拦截器
2015-07-01 15:03
537 查看
作者:ssslinppp ![](https://images0.cnblogs.com/blog/731047/201507/011501537188137.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501548757450.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501555468078.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501563432435.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501585783319.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502000314074.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502011873387.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502031401759.png)
中断流程
![](https://images0.cnblogs.com/blog/731047/201507/011502037814929.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502050003457.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502064212456.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502073122083.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502083433369.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502093437197.png)
总共定义了5个拦截器
![](https://images0.cnblogs.com/blog/731047/201507/011502103907010.png)
每个拦截器中只是打印消息,各拦截器类似,程序如下:public class MyInterceptor1 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor1-preHandle()"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav) throws Exception { System.out.println("MyInterceptor1-postHandle()"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn) throws Exception { System.out.println("MyInterceptor1-afterCompletion()"); }}
配置拦截器<!-- 定义拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截指定请求 --> <mvc:mapping path="/test/interceptor12" /> <bean class="com.ll.Interceptor.MyInterceptor1" /> </mvc:interceptor> <!-- 拦截所有的请求 --> <mvc:interceptor> <mvc:mapping path="/test/*" /> <bean class="com.ll.Interceptor.MyInterceptorAll" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor12" /> <bean class="com.ll.Interceptor.MyInterceptor2" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor34" /> <bean class="com.ll.Interceptor.MyInterceptor3" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor34" /> <bean class="com.ll.Interceptor.MyInterceptor4" /> </mvc:interceptor> </mvc:interceptors>
特别注意拦截器的配置顺序,如下:MyInterceptor1;
MyInterceptorAll;
MyInterceptor2;
MyInterceptor3;
MyInterceptor4;
其中,MyInterceptor1和MyInterceptor2拦截interceptor12请求,MyInterceptor3和MyInterceptor4拦截interceptor34请求,MyInterceptorAll拦截所有请求。
![](https://images0.cnblogs.com/blog/731047/201507/011502114847108.png)
http://localhost:8080/SpringMVCTest/test/interceptor34
![](https://images0.cnblogs.com/blog/731047/201507/011502120468237.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502127813379.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502137183694.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502142651594.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502154378433.png)
来自为知笔记(Wiz)
1. 拦截器简介及应用场景
![](https://images0.cnblogs.com/blog/731047/201507/011501537188137.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501548757450.png)
2. 拦截器接口及拦截器适配器[/b]
![](https://images0.cnblogs.com/blog/731047/201507/011501555468078.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501563432435.png)
![](https://images0.cnblogs.com/blog/731047/201507/011501585783319.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502000314074.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502011873387.png)
3. 运行流程图
正常运行![](https://images0.cnblogs.com/blog/731047/201507/011502031401759.png)
中断流程
![](https://images0.cnblogs.com/blog/731047/201507/011502037814929.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502050003457.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502064212456.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502073122083.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502083433369.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502093437197.png)
4. 程序实例
控制层:@Controller@RequestMapping(value = "/test")public class TestController { @RequestMapping(value = "/interceptor12") public String interceptor12() { System.out.println("Controller层方法-interceptor12"); return "index"; } @RequestMapping(value = "/interceptor34") public String interceptor34() { System.out.println("Controller层方法-interceptor34"); return "index"; }}总共定义了5个拦截器
![](https://images0.cnblogs.com/blog/731047/201507/011502103907010.png)
每个拦截器中只是打印消息,各拦截器类似,程序如下:public class MyInterceptor1 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor1-preHandle()"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav) throws Exception { System.out.println("MyInterceptor1-postHandle()"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn) throws Exception { System.out.println("MyInterceptor1-afterCompletion()"); }}
配置拦截器<!-- 定义拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截指定请求 --> <mvc:mapping path="/test/interceptor12" /> <bean class="com.ll.Interceptor.MyInterceptor1" /> </mvc:interceptor> <!-- 拦截所有的请求 --> <mvc:interceptor> <mvc:mapping path="/test/*" /> <bean class="com.ll.Interceptor.MyInterceptorAll" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor12" /> <bean class="com.ll.Interceptor.MyInterceptor2" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor34" /> <bean class="com.ll.Interceptor.MyInterceptor3" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test/interceptor34" /> <bean class="com.ll.Interceptor.MyInterceptor4" /> </mvc:interceptor> </mvc:interceptors>
特别注意拦截器的配置顺序,如下:MyInterceptor1;
MyInterceptorAll;
MyInterceptor2;
MyInterceptor3;
MyInterceptor4;
其中,MyInterceptor1和MyInterceptor2拦截interceptor12请求,MyInterceptor3和MyInterceptor4拦截interceptor34请求,MyInterceptorAll拦截所有请求。
5. 测试
http://localhost:8080/SpringMVCTest/test/interceptor12![](https://images0.cnblogs.com/blog/731047/201507/011502114847108.png)
http://localhost:8080/SpringMVCTest/test/interceptor34
![](https://images0.cnblogs.com/blog/731047/201507/011502120468237.png)
6. 其他应用举例-登录检测
![](https://images0.cnblogs.com/blog/731047/201507/011502127813379.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502137183694.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502142651594.png)
![](https://images0.cnblogs.com/blog/731047/201507/011502154378433.png)
7. 其他
博客:http://www.cnblogs.com/ssslinppp http://blog.sina.com.cn/spstudy 淘宝-代做毕设:http://shop110473970.taobao.com/?spm=a230r.7195193.1997079397.42.AvYpGW http://shop125186102.taobao.com/?spm=a1z10.1-c.0.0.SsuajD来自为知笔记(Wiz)
附件列表
相关文章推荐
- springMVC读书笔记--spring启动时web初始化
- java解压缩zip文件
- java 面向对象 7_1
- java基础之异常
- 使用Eclipse+NDK编译arm64-v8a CPU架构的speex.so文件
- spring data jpa
- 《Java并发编程实践》笔记6——并发性调优
- Java死锁范例以及如何分析死锁
- JAVA字符串的优化
- Java Exception
- Java基础复习(二)
- 关于java String的“==”和equals()比较、引用的小问题
- Java集合框架List,Map,Set等全面介绍
- 解决Eclipse启动闪退的最佳办法
- Java 基础入门随笔(6) JavaSE版——数组操作
- java压缩文件夹或文件
- JDK安装与环境变量配置
- unknow error:java.lang.NullPointerException
- JAVA8 StringJoiner类
- JAVA8 String.join 字符串拼接