SpringMVC之拦截器

自定义拦截器概述

pringMVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器可以实现HandlerInterceptor接口,或者可以继承
HandlerInterceptorAdapter 适配器类

  • preHandler():这个方法再业务处理器处理请求之前被调用,再该方法中对用户请求request进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
  • postHandler():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用。在该方法中对用户请求request进行处理。
  • *afterCompletion():在 DispatcherServlet 完全处理完请求后被调用。可以在该方法中进行一些资源清理的操作。

单个拦截器

public class FirstHandlerInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-afterCompletion");
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-postHandle");
		
	}

	@Override
	/*
	 * 返回true:
	 *   	com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-preHandle
     * 		com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-postHandle
	 *		com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-afterCompletion
	 *返回false:
	 *      com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor-preHandle
	 * */
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println(this.getClass().getName() + "-preHandle");
		return false;
	}
    
}
  • 配置文件
	<mvc:interceptors>
		<bean id="FirstHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor"></bean>
	</mvc:interceptors>

在这里插入图片描述

多个拦截器

public class FirstHandlerInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-afterCompletion");
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-postHandle");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println(this.getClass().getName() + "-preHandle");
		return false;
	}
    
}
public class FirstHandlerInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-afterCompletion");
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println(this.getClass().getName() + "-postHandle");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println(this.getClass().getName() + "-preHandle");
		return true;
	}
    
}
  • 配置文件
	<mvc:interceptors>
		<bean id="FirstHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.FirstHandlerInterceptor"></bean>
		<bean id="SecondHandlerInterceptor" class="com.oceanstar.springmvc.helloworld.SecondHandlerInterceptor"></bean>
	</mvc:interceptors>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值