zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    SQL中获取自增长的最大ID
    (inline)内联函数在IOS开发中的使用
    MS SQL SERVER 2005 高可用性之日志传送
    19_toast通知和notify通知 onTouch事件响应
    20 按比例设置 子控件的宽度和高度
    18_SurfaceView 其他线程绘图
    使用Microsoft Media Service实现网络影音多媒体应用系列第三篇技术要点
    使用Microsoft Media Service实现网络影音多媒体应用系列第二篇开发须知
    MVC3WIN7下的IIS7.5部署MVC3应用程序
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10184375.html
Copyright © 2011-2022 走看看