zoukankan      html  css  js  c++  java
  • StringMVC(拦截器)

    单个拦截器

    使用jar包

    创建FirstController.java

    @Controller
    public class FirstController {
        @RequestMapping("/first.do")
         public String doFirst(){
             return "/index.jsp";
         }
    }

     创建连接器类FirstInterceptor.java

    /**
     * Created by Happy on 2017-01-12.  alt+Enter 自动导入包
     */
    public class FirstInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            System.out.println("==================preHandle===============");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            System.out.println("==================postHandle===============");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            System.out.println("==================afterCompletion===============");
        }
    }

    编写applicationContext.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:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            ">
         
       <!-- 配置包扫描器-->
           <context:component-scan base-package="cn.happy.interceptor"></context:component-scan>
    
    
        <!--配置拦截器-->
         <mvc:interceptors>
             <mvc:interceptor>
                 <mvc:mapping path="/**" />
                 <bean class="cn.happy.interceptor.FirstInterceptor"></bean>
             </mvc:interceptor>
         </mvc:interceptors>
    </beans>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <!--核心控制器-->
        <servlet>
            <servlet-name>sprigmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>sprigmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    </web-app>

     单个拦截器请求过程图

    多个拦截器

    在单个拦截器的基础上多添加一个连接器类SFirstInterceptor.java

    /**
     * Created by Happy on 2017-01-12.  alt+Enter 自动导入包
     */
    public class FirstInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            System.out.println("=========S=========preHandle===============");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            System.out.println("=========S=========postHandle===============");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            System.out.println("==========S========afterCompletion===============");
        }
    }

    更改applicationContext.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:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            ">
         
       <!-- 配置包扫描器-->
           <context:component-scan base-package="cn.happy.interceptor"></context:component-scan>
    
    
        <!--配置攔截器-->
         <mvc:interceptors>
             <mvc:interceptor>
                 <mvc:mapping path="/**" />
                 <bean class="cn.happy.interceptor.FirstInterceptor"></bean>
             </mvc:interceptor>
             <mvc:interceptor>
                 <mvc:mapping path="/**" />
                 <bean class="cn.happy.interceptor.SFirstInterceptor"></bean>
             </mvc:interceptor>
         </mvc:interceptors>
    </beans>

    多拦截器请求过程图

  • 相关阅读:
    hdu 3268 09 宁波 现场 I
    hdu 3697 10 福州 现场 H
    CodeForces Round #521 (Div.3) D. Cutting Out
    #Leetcode# 226. Invert Binary Tree
    zufe 蓝桥选拔
    #Leetcode# 100. Same Tree
    #Leetcode# 6. ZigZag Conversion
    PAT 1084 外观数列
    #Leetcode# 38. Count and Say
    #Leetcode# 22. Generate Parentheses
  • 原文地址:https://www.cnblogs.com/baixingqiang/p/6285985.html
Copyright © 2011-2022 走看看