spring mvc 拦截器

说明:仅供自己学习理解笔记使用

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>

 

 

转载于:https://www.cnblogs.com/hwj2wj/archive/2013/03/26/2981919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值