springmvc-210807-03—多个拦截器实现
多个拦截器执行顺序
多个拦截器执行顺序,
是按照声明的顺序执行。
<mvc:interceptors>
<!-- 第一个拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.bgy.handler.MyInterceptor"/>
</mvc:interceptor>
<!-- 第二个拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.bgy.handler.MyInterceptor02"/>
</mvc:interceptor>
</mvc:interceptors>
先1后2
多个拦截器设置不同preHandle情况
案例01:
拦截器01--preHandle=true,拦截器02--preHandle=true
运行结果:
111111这是MyInterceptor01的preHandler()方法111111
222222这是MyInterceptor02的preHandler()方法222222
这是MyController的doSome()方法
222222这是MyInterceptor02的postHandle()方法222222
111111这是MyInterceptor01的postHandle()方法111111
222222这是MyInterceptor02的afterCompletion()方法222222
111111这是MyInterceptor01的afterCompletion()方法111111
案例02:
拦截器01--preHandle=true,拦截器02--preHandle=false
运行结果:
111111这是MyInterceptor01的preHandler()方法111111
222222这是MyInterceptor02的preHandler()方法222222
111111这是MyInterceptor01的afterCompletion()方法111111
案例03:
拦截器01--preHandle=false,拦截器02--preHandle=true
运行结果:
111111这是MyInterceptor01的preHandler()方法111111
演示多个拦截器(只贴出和springmvc-210807-02—一个拦截器实现不一样的代码)
MyInterceptor01.java(拦截器01)
package com.bgy.handler;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
public class MyInterceptor01 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("111111这是MyInterceptor的preHandler()方法111111");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mv) throws Exception {
System.out.println("111111这是MyInterceptor的postHandle()方法111111");
if (mv != null) {
mv.addObject("mydate",new Date());
mv.setViewName("other");
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("111111这是MyInterceptor的afterCompletion()方法111111");
}
}
MyInterceptor02.java(拦截器02)
package com.bgy.handler;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
public class MyInterceptor02 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("222222这是MyInterceptor的preHandler()方法222222");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mv) throws Exception {
System.out.println("222222这是MyInterceptor的postHandle()方法222222");
if (mv != null) {
mv.addObject("mydate",new Date());
mv.setViewName("other");
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("222222这是MyInterceptor的afterCompletion()方法222222");
}
}
dispatchServlet.xml(声明拦截器01,拦截器02)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bgy.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.bgy.handler.MyInterceptor01"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.bgy.handler.MyInterceptor02"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>