HandlerInterceptor类加载配置有两种方式,分别是“针对HandlerMapping配置“和”全局配置“。
针对Handler Mapping配置:
<!-- 拦截器:针对HandlerMapping配置 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptor1"></ref>
<ref bean="myInterceptor2"></ref>
</list>
</property>
</bean>
<bean id="myInterceptor1" class="com.interceptor.MyInterceptor1"></bean>
<bean id="myInterceptor2" class="com.interceptor.MyInterceptor2"></bean>
这种配置的优点是针对具体的处理器映射器进行拦截操作。
针对全局配置:
<!--配置拦截器:针对全局配置 -->
<mvc:interceptors>
<!-- 使用bean直接定义在<mvc:interceptors>下面的拦截器将拦截所有请求 -->
<!-- 这个是全局的,当使用下面的拦截器1和拦截器2时,需要把这个拦截器注释掉 -->
<bean class="com.springmvc.interceptor.MyInterceptor"/>
<!-- 定义多个拦截器,顺序执行 -->
<mvc:interceptor> <!-- 拦截器1 -->
<mvc:mapping path="/**"/> <!-- 配置拦截器作用的路径 /**表示拦截所有url路径包括子url路径-->
<mvc:exclude-mapping path=""/> <!-- 配置不需要拦截器作用的路径 -->
<!-- 定义在<mvc:interceptor>下面的拦截器,表示匹配路径请求才进行拦截 -->
<bean class="com.springmvc.interceptor.MyInterceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor> <!-- 拦截器2 -->
<mvc:mapping path="/hello" /> <!-- 配置拦截器作用的路径 -->
<!-- 定义在<mvc:interceptor>下面的拦截器,表示匹配路径请求才进行拦截 -->
<bean class="com.springmvc.interceptor.MyInterceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>