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>

    多拦截器请求过程图

  • 相关阅读:
    如何在局域网安装Redmine(转贴)
    介绍开源的项目管理系统-Redmine
    【转】ios开发证书,描述文件,bundle ID的关系
    【转】Spring+Mybatis+SpringMVC+Maven+MySql搭建实例
    【转】 Tomcat+redis+nginx配置
    【转】Java做服务器开发语言
    【转】程序员的五大陷阱
    一个简单的网页服务器
    【转】Eclipse中创建并运行Servlet项目
    【转】在Eclipse环境下配置Servlet开发环境
  • 原文地址:https://www.cnblogs.com/baixingqiang/p/6285985.html
Copyright © 2011-2022 走看看