会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
我才是听雨轩
博客园
首页
新随笔
联系
订阅
管理
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
posted @
2013-05-09 18:22
深海巨坑
阅读(
331
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告