说明:仅供自己学习理解笔记使用
spring mvc 拦截器需要实现HandlerInterceptor接口
public class UserIsLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("Pre-handle");
return false;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("Post-handle");
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("After completion handle");
}
}
模拟一个控制器:
@Controller
public class AddUserController {
@RequestMapping(value = "/user/addUi", method = RequestMethod.GET)
public String addUi() {
return "addUser";
}
}
spring的配置:
<?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:annotation-config />
<context:component-scan base-package="net.viralpatel.spring3.controller"/>
<!--注册spring mvc 拦截器-->
<mvc:interceptors>
<bean class="com.yjdgis.interceptor.UserIsLoginInterceptor" />
</mvc:interceptors>
<!--多个拦截器的注册-->
<!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="com.yjdgis.interceptor.handlerInterceptor1"/>
<ref bean="com.yjdgis.interceptor.handlerInterceptor2"/>
</list>
</property>
</bean> -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
则拦截器的执行顺序就是此处添加拦截器的顺序;会输出:
HandlerInterceptor1 preHandle
HandlerInterceptor2 preHandle
HandlerInterceptor2 postHandle
HandlerInterceptor1 postHandle
HandlerInterceptor2 afterCompletion
HandlerInterceptor1 afterCompletion
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
附加说明:
spring配置文件注册拦截器一
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yjdgis.interceptors.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
spring拦截器注册二
bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<bean class="com.yjdgis.interceptors.MyInterceptor"></bean>
</list>
</property>
</bean>
在方法二中不可使用<mvc:annotation-driven />。 它默认已经注册了DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
<!-- 包扫描,注册了各种处理器,<mvc:annotation-driven />也有可能重复注册好多处理器。只有我们都熟悉了,再使用完全自动化吧 -->
<context:component-scan base-package="com.yjdgis"/>
多拦截器注册:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yjdgis.interceptors.MyInterceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yjdgis.interceptors.MyInterceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>