Spring MVC多个拦截器(Interceptor)的执行流程(13)

    技术2022-07-10  106

    Spring MVC多个拦截器(Interceptor)的执行流程

    多个拦截器的执行流程拦截器tesetInterCepeter1spring-config.xml测试

    多个拦截器的执行流程

    在 Web 应用中通常需要有多个拦截器同时工作,这时它们的 preHandle 方法将按照配置文件中拦截器的配置顺序执行,而它们的 postHandle 方法和 afterCompletion 方法则按照配置顺序的反序执行。

    拦截器tesetInterCepeter1

    package interCeptor; import org.apache.log4j.Logger; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class tesetInterCepeter1 implements HandlerInterceptor { private static Logger log = Logger.getLogger(testInterCepetor.class.getClass()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.debug("tesetInterCepeter1 preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.debug("tesetInterCepeter1 postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.debug("tesetInterCepeter1 afterCompletion"); } }

    spring-config.xml

    <mvc:interceptors> <!-- 拦截所有请求--> <bean class="interCeptor.testInterCepetor"/> <mvc:interceptor> <!-- 拦截所有路径--> <mvc:mapping path="/**"/> <!-- 剔除不拦截内容--> <mvc:exclude-mapping path="/test"/> <bean class="interCeptor.tesetInterCepeter1"/> </mvc:interceptor> </mvc:interceptors>

    测试

    Processed: 0.008, SQL: 9