當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【Spring学习笔记-MVC-17】Spring MVC之拦截器
生活随笔
收集整理的這篇文章主要介紹了
【Spring学习笔记-MVC-17】Spring MVC之拦截器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:ssslinppp ? ? ?
中斷流程
@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個攔截器
每個攔截器中只是打印消息,各攔截器類似,程序如下:
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攔截所有請求。
http://localhost:8080/SpringMVCTest/test/interceptor34?
博客: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)
1. 攔截器簡介及應用場景
2. 攔截器接口及攔截器適配器
3. 運行流程圖
正常運行中斷流程
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個攔截器
每個攔截器中只是打印消息,各攔截器類似,程序如下:
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>???
特別注意攔截器的配置順序,如下:
5. 測試
http://localhost:8080/SpringMVCTest/test/interceptor12?http://localhost:8080/SpringMVCTest/test/interceptor34?
6. 其他應用舉例-登錄檢測
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)
附件列表
?
轉載于:https://www.cnblogs.com/ssslinppp/p/4613273.html
總結
以上是生活随笔為你收集整理的【Spring学习笔记-MVC-17】Spring MVC之拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习计划—4-23
- 下一篇: 谈谈自己对于Auth2.0的见解