zoukankan
html css js c++ java
Spring MVC 拦截器
转自Spring MVC 拦截器
Controller层的拦截器继承于HandlerInterceptorAdapter
HandlerInterceptorAdapter.java
1
public
abstract
class
HandlerInterceptorAdapter
implements
HandlerInterceptor {
2
3
/**
4
* This implementation always returns <code>true</code>.
5
*/
6
public
boolean
preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
7
throws
Exception {
8
return
true
;
9
}
10
11
/**
12
* This implementation is empty.
13
*/
14
public
void
postHandle(
15
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
16
throws
Exception {
17
}
18
19
/**
20
* This implementation is empty.
21
*/
22
public
void
afterCompletion(
23
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
24
throws
Exception {
25
}
26
27
}
Spring拦截器通过重写这三个方法实现Controller的拦截。
配置拦截器
xml
1
<
bean id
=
"
handlerMapping
"
2
class
=
"
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
"
>
3
<
property name
=
"
interceptors
"
>
4
<
list
>
5
<
ref bean
=
"
controllerInterceptor
"
/>
6
</
list
>
7
</
property
>
8
<
property name
=
"
mappings
"
>
9
<
props
>
10
<
prop key
=
"
/hao/hello.do
"
>
helloWorldController
</
prop
>
11
</
props
>
12
</
property
>
13
</
bean
>
14
15
<
bean id
=
"
controllerInterceptor
"
class
=
"
com.web.spring.mvc.interceptor.ControllerInterceptor
"
/>
16
ControllerInterceptor.java
1
public
class
ControllerInterceptor
extends
HandlerInterceptorAdapter {
2
3
/**
4
* 在Controller方法前进行拦截
5
*/
6
public
boolean
preHandle(HttpServletRequest request,
7
HttpServletResponse response, Object handler)
throws
Exception {
8
System.out.println(
"
ControllerInterceptor.preHandle()
"
);
9
return
true
;
10
}
11
12
/**
13
* This implementation is empty.
14
*/
15
public
void
postHandle(HttpServletRequest request,
16
HttpServletResponse response, Object handler,
17
ModelAndView modelAndView)
throws
Exception {
18
System.out.println(
"
ControllerInterceptor.postHandle()
"
);
19
}
20
21
/**
22
* 在Controller方法后进行拦截
23
*/
24
public
void
afterCompletion(HttpServletRequest request,
25
HttpServletResponse response, Object handler, Exception ex)
26
throws
Exception {
27
System.out.println(
"
ControllerInterceptor.afterCompletion()
"
);
28
}
29
}
说明:
发起请求,进入拦截器链,运行所有拦截器的preHandle方法,
1.当preHandle方法返回false时,从当前拦截器往回执行所有拦截器的afterCompletion方法,再退出拦截器链。
2.当preHandle方法全为true时,执行下一个拦截器,直到所有拦截器执行完。再运行被拦截的Controller。然后进入拦截器链,运行所有拦截器的postHandle方法,完后从最后一个拦截器往回执行所有拦截器的afterCompletion方法.
当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法
Controller
1
2
@Controller
3
@RequestMapping(
"
/hao
"
)
5
public
class
HelloWorldController {
6
7
@RequestMapping(value
=
"
/hello
"
)
8
public
String hello(HttpServletRequest request, HttpServletResponse response) {
9
System.out.println(
"
hello
"
);
15
return
"
helloWorld
"
;
16
}
17
}
posted on 2010-10-05 01:00
岁月神偷
阅读(8471)
评论(4)
编辑
收藏
所属分类:
Spring
查看全文
相关阅读:
振动传感器
水银开关式碰撞传感器
倾斜传感器
光敏传感器
激光传感器的使用
html字符串 转 json
浏览器播放视频加快进功能
处理箭头函数单个参数括号规则冲突
VUE-directive指令之语法、引用、钩子函数、参数
win10系统自动升级怎么还原到以前的系统
原文地址:https://www.cnblogs.com/yaoxing92/p/3069524.html
最新文章
C++实现链式栈,运用模板,界面友好,操作方便,运行流畅
C++实现二叉树,运用模板,界面友好,操作方便 运行流畅
C++实现有向权图的基本操作,界面友好,操作方便,运行流畅
C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
JAVA的i++, i+=1, i=i+1有区别吗?
linux下的vim使用笔记
自己搭建一个记笔记的环境记录(leanote)
window下安装FTP服务器
git 入门学习笔记
用SecureCRT连接虚拟机中的Linux系统(Ubuntu)
热门文章
css权重计算方法浅谈
Ajax 异步交互
Canvas 入门案例
JavaScript 入门案例
CSS3
CSS
HTML
SICP练习1.6-1.8
SICP中sqrt(开方)的实现(附C#实现)
DH11数字温湿度传感器
Copyright © 2011-2022 走看看