关于spring:SpringMVC拦截器

6次阅读

共计 3456 个字符,预计需要花费 9 分钟才能阅读完成。

后期常识

  • 拦截器的作用: 拦截器的作用是在服务器解决申请资源之前、之后去做一些事件,比方在解决申请资源之前须要验证一下从前端传入过去的数据合不合规定,解决申请资源之后验证一下有没有产生异样
  • 拦截器的三个办法:

    • preHandle:在解决申请资源之前做的办法

      • 当返回值为 true,示意放行,也就是能够解决申请资源
      • 当返回值为 false,示意不放行,也就是不能解决申请资源
    • postHandle:在解决申请资源之后做的办法,如果 preHandle 不放行,那么这个办法也肯定不会执行到
    • afterCompletion:是一个最终执行的办法,如果是只有一个拦截器可能看不到成果,然而当有多个拦截器时,会先执行多个拦截器的 preHandle 和 postHandle,而后再执行 afterCompletion
  • 拦截器与 filter 的触发地位:

    • filter:在咱们应用 Tomcat 的时候,它给咱们提供了 filter 过滤器,这个 filter 的触发地位是位于浏览器和服务器之间,也就是当申请的资源还没达到服务器时,filter 就先对申请的资源做解决
    • 拦截器:而拦截器是 SpringMVC 框架提供的,既然是框架提供的,那么当申请还没达到服务器时 SpringMVC 是没有方法解决的,也就是当 SpringMVC 接管到申请之后,它会先把申请资源交给拦截器,而后再放行给解决具体申请资源的 Servlet 类
  • 拦截器与 filter 的拦挡范畴

    • filter:什么都可能拦挡,既能拦挡 Servlet 资源,也能拦挡 html,css,jsp 等文件
    • 拦截器:只能拦挡 Servlet 资源

一、拦截器的配置与应用

  • 自定义拦截器类

        public class MyInterceptor implements HandlerInterceptor {
            // 事后解决,在资源解决前
             @Override
             public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("interceptor1:资源解决前");
                    return true;// 放行
             }
                // 后置解决,在资源解决后
             @Override
             public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("interceptor1:资源解决后");
             }
                // 最终实现后处理
             @Override
             public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("interceptor1:最终办法");
             }
        }
  • Controller 执行类

    @Controller
    public class UserController {@RequestMapping("testIntercept.do")
        public String testIntercept(){System.out.println("controller 资源解决了");
            return "";
        }
    }
  • JSP

    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    <html>
    <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>cai jin hong</title>
    </head>
    <body>
     <a href="testIntercept.do"> 拦截器 </a>
    </body>
    </html>
  • Spring 配置文件

    <?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:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 手动配置这个类的驱动 -->
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
        <!-- 配置拦截器 -->
         <mvc:interceptors>
             <mvc:interceptor>
                 <mvc:mapping path="/*"/>
                 <bean class="com.cjh.interceptor.MyInterceptor"></bean>
             </mvc:interceptor>
             <mvc:interceptor>
                 <mvc:mapping path="/*"/>
                 <bean class="com.cjh.interceptor.MyInterceptor2"></bean>
             </mvc:interceptor>
         </mvc:interceptors>
    </beans>
  • 执行后果

二、拦截器的执行流程

1、一个拦截器

如下面代码所示,

  • 拦截器会先执行 preHandle 办法,如果 preHandle 返回的后果为 true,那么执行具体执行申请资源的 Controller 类
  • 而后再执行 postHandle 办法
  • 最初执行 afterCompletion 办法

2、多个拦截器

假如有两个拦截器,依据配置信息的配置地位,拦截器 1 的配置信息在拦截器 2 的后面,那么首先会执行拦截器 1

  • 1)拦截器 1 先执行 preHandle 办法

    • 如果 preHandle 返回 true,拦截器 2 执行 preHandle 办法,跳到第 2 步
    • 如果 preHandle 返回 false,拦截器 2 不执行 preHandle 办法,跳到第 7 步
  • 2)当拦截器 2 的 preHandle 办法返回:

    • true,执行 controller 的办法,跳到第 3 步
    • false,跳到第 6 步
  • 3)拦截器 2 执行 postHandle 办法
  • 4)拦截器 1 执行 postHandle 办法
  • 5)拦截器 2 执行 afterCompletion 办法
  • 6)拦截器 1 执行 afterCompletion 办法
  • 7)完结
  • 留神:只有某个拦截器的 preHandle 办法放行了(返回 true),那么不管它的 postHandle 办法能不能执行,它的 afterCompletion 办法肯定会执行,这就有点像 try{}catch{}finally{}
  • 多个拦截器的代码和单个的是一样的,只是配置的时候须要把每个拦截器都配置进去,每个拦截器是一个 interceptor 标签对象
正文完
 0