springmvc

在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下。

preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Controller实现);
     返回值:true表示继续流程(如调用下一个拦截器或处理器);
             false表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器,此时我们需要通过response来产生响应;
postHandle:后处理回调方法,实现处理器的后处理(但在渲染视图之前),此时我们可以通过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行处理,modelAndView也可能为null。
afterCompletion:整个请求处理完毕回调方法,即在视图渲染完毕时回调,如性能监控中我们可以在此记录结束时间并输出消耗时间,还 可以进行一些资源清理,类似于try-catch-finally中的finally,但仅调用处理器执行链中preHandle返回true的拦截器的 afterCompletion。

  下面是简单的DEMO

1)控制器

@Controller
public class TestController {
    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String handlerString(){
        System.out.println("进入处理器");
        return "hello";
    }
}


2) 拦截器

public class TestInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        System.out.println("After completion handle");        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        
        System.out.println("Post-handle");
    }

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Pre-handle");
        return true;
    }

}

 

3) spring mvc配置文件
 

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:component-scan base-package="com.springmvc.interceptor.controller"/> 
     
    <mvc:annotation-driven/> 
    <!-- 配置拦截器,近视全局 -->
     <mvc:interceptors>  
      <bean class="com.springmvc.interceptor.TestInterceptor" />  
    </mvc:interceptors>  
     
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean> 
    <!-- 自定义拦截 -->
     <!--
     <mvc:interceptors>  
        <mvc:interceptor>  
            <mvc:mapping path="/supplier/*"/>  
            <mvc:mapping path="/goods/*"/>  
            <mvc:mapping path="/contact/*"/>  
            <bean class=""></bean>  
        </mvc:interceptor>  
    </mvc:interceptors>-->
   
</beans> 

其中使用了<mvc:interceptors>注册了拦截器


4) 运行后,输出:
Pre-handle
Post-handle
After completion handle

   如果在拦截器中的prehandler中返回false,则根本不会渲染到view层了,直接只输出:
Pre-handle,假如注册了两个拦截器,

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">    
    <property name="interceptors">    
        <list>    
           <ref bean="handlerInterceptor1"/>    
          <ref bean="handlerInterceptor2"/>    
        </list>    
    </property>    
</bean> 

 

则拦截器的执行顺序就是此处添加拦截器的顺序;会输出:
HandlerInterceptor1 preHandle 
HandlerInterceptor2 preHandle 
 
HandlerInterceptor2 postHandle 
HandlerInterceptor1 postHandle 

HandlerInterceptor2 afterCompletion 
HandlerInterceptor1 afterCompletion 
    总结下,就是在哪个拦截器的prehandler方法中返回false,则该拦截器posthandler和aftercomplete方法了。

posted on 2014-04-01 16:47  魂牵梦莹  阅读(286)  评论(0)    收藏  举报