wmcoo
岁月神偷
:: :: :: :: ::
管理
<
2024年12月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
文章分类
(22)
Hibernate(4)
J2EE(3)
Java(1)
JavaScript
MySQL
Spring(13)
SQLServer(1)
Struts
WebService
搜索
最新评论
1. re: Spring 声明式事务,propagation属性列表
可以对一些专业名词在做具体分析,就完美啦
--野狼
2. re: Spring 在配置中使用*.properties
王二伟
--儿
3. dsad
fdsfa
--fdsaf
4. re: Spring MVC 的请求参数获取
评论内容较长,点击标题查看
--zuidaima
5. re: Spring MVC 拦截器
评论内容较长,点击标题查看
--legend
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
岁月神偷
阅读(21585)
评论(6)
编辑
收藏
所属分类:
Spring
Feedback
#
re: Spring MVC 拦截器
2011-08-08 15:27 |
paladin1988
清晰明了,简单易懂
nice,我喜欢
回复
更多评论
#
re: Spring MVC 拦截器
2012-06-01 18:09 |
挺好的
挺好的
回复
更多评论
#
re: Spring MVC 拦截器
2012-07-11 12:10 |
潘洪飞
请问你那个xml配置文件中的handlerMapping应该是在哪出现的,也就是说我按照你说的,就报creating bean with name 'handlerMapping' defined in URL然后就是No bean named 'IndexController' is defined这两个错误怎么解决呢
回复
更多评论
#
re: Spring MVC 拦截器
2014-04-20 10:16 |
最代码
Spring MVC拦截器配置及其原理分析
代码下载地址:
http://www.zuidaima.com/share/1751865167973376.htm
回复
更多评论
#
re: Spring MVC 拦截器
2014-08-13 12:52 |
legend
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
怎么提高此方法的优先级,我在抛出异常的时候,异常提前被拦截封装,没有在拦截器中捕获到异常,我现在需要在拦截器中捕获异常!
回复
更多评论
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
相关文章:
Java类获取spring 容器的bean
Spring 自定义搜索,注解BEAN
Spring 注解@Transactional
Spring 声明式事务,propagation属性列表
Spring AOP管理事务
Spring MVC 拦截器
SpringMVC 中的数据验证
Spring MVC的自动转换功能 HttpMessageConverter
Spring MVC 的请求参数获取
Spring 的MVC @RequestMapping