天天看点

springMVC_10拦截器

一,简介

  1. 拦截器概念和struts概念一致

  2. 实现拦截器

      实现HandlerInterceptor接口

      配置拦截器

    <mvc:interceptors>
          <mvc:interceptor>
              <mvc:mapping path="/**"/>
              <bean class="com.ahd.interceptor.HelloInterceptor"/>
          </mvc:interceptor>
      </mvc:interceptors>      

二,具体实现

  

  拦截器 

package com.ahd.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 HelloInterceptor implements HandlerInterceptor{

    /**
     * 在dispatcherServlet处理后执行,----清理工作
     */
    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("在dispatcherServlet处理后执行,----清理工作");
        
    }
    /**
     * 在请求方法之后执行
     */
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("在请求方法之后执行");
    }
    
    /***
     * 在请求方法之前执行
     * 如果返回true,则继续执行下一个拦截器,如果返回false,则不执行下一个拦截器
     */
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("处理请求前");
        return true;
    }
    
}      

  配置文件

<?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:p="http://www.springframework.org/schema/p"
    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">
    <!-- 配置handerAdapter  适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
   <!-- 文件上传配置 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置编码格式 -->  
        <property name="defaultEncoding" value="utf-8"></property> 
        <!-- 设置文件大小 -->  
        <property name="maxUploadSize" value="10485760000"></property>
        <!-- 设置缓冲区大小 -->  
        <property name="maxInMemorySize" value="40960"></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"/>
        <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
    </bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.ahd.interceptor.HelloInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    
    <!-- spring容器扫描指定包下的所有类,如果类上有注解  那么根据注解产生相应bean对象已经映射信息 -->
    <context:component-scan base-package="com.ahd.controller"/>
    
</beans>