springmvc拦截器配置文件

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值