php mvc框架拦截器,Springmvc Interceptor 拦截器

在查看SpringMvc DispatcherServelt 源码中会发现

HandlerExecutionChain 类中

//类在创建的时候,自动加载了HandlerInterceptor 接口实现类,构建成interceptors List集合

publicHandlerExecutionChain(Object handler,HandlerInterceptor[] interceptors) {

if(handler instanceofHandlerExecutionChain) {

HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;this.handler = originalChain.getHandler();this.interceptorList = newArrayList();CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);} else{

this.handler = handler;this.interceptors = interceptors;}

}

boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {

if (getInterceptors() != null) {

for (int i = 0; i < getInterceptors().length; i++) {

HandlerInterceptor interceptor = getInterceptors()[i];

if (!interceptor.preHandle(request, response, this.handler)) {

triggerAfterCompletion(request, response, null);

return false;

}

this.interceptorIndex = i;

}

}

return true;

}

void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {

if (getInterceptors() == null) {

return;

}

for (int i = getInterceptors().length - 1; i >= 0; i--) {

HandlerInterceptor interceptor = getInterceptors()[i];

interceptor.postHandle(request, response, this.handler, mv);

}

}

void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)

throws Exception {

if (getInterceptors() == null) {

return;

}

for (int i = this.interceptorIndex; i >= 0; i--) {

HandlerInterceptor interceptor = getInterceptors()[i];

try {

interceptor.afterCompletion(request, response, this.handler, ex);

}

catch (Throwable ex2) {

logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);

}

}

}

在上面3个方法中循环,调用了HandlerInterceptor 实现类中 preHandle,postHandle,afterCompletion,中的3个方法

以下是我的测试

importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;

importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;

public classZdyHandlerInterceptor implementsHandlerInterceptor {

Logger log= LogManager.getLogger(ZdyHandlerInterceptor.class.getName());

//整个请求结束之后,modelView已完成渲染,进行的操作例如回收资源@Overridepublic voidafterCompletion(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,Exception arg3)

throwsException {

log.info("后置拦截器");//TODO Auto-generated method stub}

//在Controller处理业务之后,返回modelView,但还没对前端进行渲染操作,也就是说在此方法中我们可以对返回的modelView做些操作@Overridepublic voidpostHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,ModelAndView arg3)

throwsException {

//TODO Auto-generated method stublog.info("处理器");}

//前置拦截器,默认返回false,只有返回true的时候才会执行后面的两个方法@Overridepublic booleanpreHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2) throwsException {

//TODO Auto-generated method stublog.info("前置拦截器");return false;}

}

SpringMvc配置文件

测试结果

INFO -> (http-nio-9080-exec-7) - 前置拦截器

preHandle 改为true后

INFO -> (http-nio-9080-exec-10) - 前置拦截器 INFO -> (http-nio-9080-exec-10) - 处理器 INFO ->(http-nio-9080-exec-10) - 后置拦截器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值