码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC之拦截器

时间:2014-08-20 01:24:45      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   os   io   2014   cti   

有两种方法配置spring的拦截器

1. 实现接口: HandleInterceptor

public class MyInterceptor1 implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {
		System.out.println("最后执行!!!一般用于释放资源!!");
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView model) throws Exception {
		System.out.println("Action执行之后,生成视图之前执行!!");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		System.out.println("action之前执行!!!");
		return true; // 继续执行action
	}

}


2. 继承适配器: HandleInterceptorAdapter

public class MyInterceptor2 extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		System.out.println("MyInterceptor2.preHandle()");
		return true; // 继续执行action
	}

}

3. 配置user-servlet.xml

<!-- 配置自定义拦截器 -->
<mvc:interceptors>
	<bean class="com.zdp.interceptor.MyInterceptor1"></bean> <!-- 拦截所有的url! -->
	<mvc:interceptor>
		<mvc:mapping path="/user/add" /> <!-- 只拦截添加用户 -->
		<bean class="com.zdp.interceptor.MyInterceptor2"></bean>
	</mvc:interceptor>
</mvc:interceptors>



springMVC之拦截器,布布扣,bubuko.com

springMVC之拦截器

标签:style   blog   http   java   os   io   2014   cti   

原文地址:http://blog.csdn.net/zdp072/article/details/38693049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!