servlet/filter/listener/interceptor区别与联系
- 创建Interceptor
package com.company.combine.intercepter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by liuqun on 2017/8/17.
*/
public class MyCustomerIntercepter1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println(getClass().getName()+" preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println(getClass().getName()+" postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println(getClass().getName()+" afterCompletion");
}
}
package com.company.combine.intercepter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by liuqun on 2017/8/17.
*/
public class MyCustomerIntercepter2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println(getClass().getName()+" preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println(getClass().getName()+" postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println(getClass().getName()+" afterCompletion");
}
}
- 在配置文件中配置:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.company.combine.intercepter.MyCustomerIntercepter1"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.company.combine.intercepter.MyCustomerIntercepter2"/>
</mvc:interceptor>
</mvc:interceptors>
- preHandle返回false则打断拦截链直接返回,后续不再执行。返回true,则继续。
3.1两个HandlerInterceptor的preHandle()方法都返回true的执行结果
com.company.combine.intercepter.MyCustomerIntercepter1 preHandle
com.company.combine.intercepter.MyCustomerIntercepter2 preHandle
com.company.combine.intercepter.MyCustomerIntercepter2 postHandle
com.company.combine.intercepter.MyCustomerIntercepter1 postHandle
com.company.combine.intercepter.MyCustomerIntercepter2 afterCompletion
com.company.combine.intercepter.MyCustomerIntercepter1 afterCompletion
3.2 MyCustomerIntercepter2的preHandle返回false执行结果
com.company.combine.intercepter.MyCustomerIntercepter1 preHandle
com.company.combine.intercepter.MyCustomerIntercepter2 preHandle
com.company.combine.intercepter.MyCustomerIntercepter1 afterCompletion
3.3 两个Intercepter的preHandle都返回false执行结果
com.company.combine.intercepter.MyCustomerIntercepter1 preHandle
网友评论