SpringMVC—拦截器

来源:互联网 发布:淘宝用户名是会员名吗 编辑:程序博客网 时间:2024/06/05 07:49
拦截器


要点:
1.拦截器要实现HandlerInterceptor接口。
2.可设置多个拦截器。


拦截器1:
package com.cjh.Interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class UserInterceptor implements HandlerInterceptor {


/**
* 用于释放资源的方法
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception)
throws Exception {


}




/**
* 控制器执行完,生成视图之前可以做的动作,向模型中添加公共成员
*/
public void postHandle(HttpServletRequest request, HttpServletResponse repsponse,
Object handler, ModelAndView modelAndView) throws Exception {


System.out.println("UserInterceptor----->postHandle");
}


/**
* 拦截之前执行的方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {

System.out.println("UserInterceptor----->preHandle");
return true;
}


}


拦截器2:
package com.cjh.Interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class UserInterceptor2 implements HandlerInterceptor {


public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {


}


public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {

System.out.println("UserInterceptor2---->postHandle2");
}


public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("UserInterceptor2---->preHandle2");
return true;
}


}


servlet.xml:
<?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"  
        xmlns:util="http://www.springframework.org/schema/util"    
        xsi:schemaLocation="   
          http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-3.0.xsd   
          http://www.springframework.org/schema/mvc       
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
          http://www.springframework.org/schema/util    
          http://www.springframework.org/schema/util/spring-util-3.0.xsd">   
        

<!-- 主键扫描,配置要扫描的Java类 -->
<context:component-scan base-package="com.cjh.action"/>


    <!-- 定义一个视图解析类,基于ResourceView的解析器 ,此解析器是在url解析器上,加入了对jstl的支持-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    
    </bean>
    
    <!-- 设置拦截器链 ,当配置了多个拦截器bean时,则是顺序的往下执行-->
    <mvc:interceptors>
        <!-- 设置拦截器 -->
    <mvc:interceptor>
     <!-- 拦截的url -->
     <mvc:mapping path="/*"/>
     <!-- 拦截器类 -->
     <bean class="com.cjh.Interceptor.UserInterceptor"/>
    </mvc:interceptor>
   
    <mvc:interceptor>
     <mvc:mapping path="/*"/>
     <bean class="com.cjh.Interceptor.UserInterceptor2"/>
    </mvc:interceptor>
    </mvc:interceptors>
    
</beans>