SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解

 ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

拦截器Interceptor:

对处理方法进行双向的拦截,可以对其做日志记录等

我选择的是实现HandlerInterceptor接口

我把执行流程放上来,单个拦截器的和多个拦截器的,然后写个Demo案例,就可以正式结束Springmvc,过几天写完SSM整合,SSM就结束了,将来有机会多放几个项目在博客园上

图1,单个拦截器执行流程

图2,多个拦截器的执行流程

我会给大家定义俩个拦截器,大家跑一下,看看执行顺序,另外大家可以举一反三的扩充,定义多个拦截器或者在拦截器的方法中填实际的处理代码

案例Demo开始

  1.自定义第一个拦截器FirstInterceptor

package cn.dawn.day25interceptor;/**
 * Created by dawn on 2018-04-02.
 */

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstInterceptor implements HandlerInterceptor{
    //在Handler执行前执行
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("preHandle=============");
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle=============");
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion=============");
    }
}

  preHandle这个方法,它的返回值决定了下面俩个方法是否执行,如果false,则不会执行下面的那俩方法

  2.自定义第二个拦截器SecondInterceptor

package cn.dawn.day25interceptor;/**
 * Created by dawn on 2018-04-02.
 */

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecondInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("Second preHandle=============");
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("Second postHandle=============");
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("Second afterCompletion=============");
    }
}

  3.自定义处理器和处理方法

package cn.dawn.day25interceptor;/**
 * Created by dawn on 2018-04-02.
 */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FirstController {
    @RequestMapping("/first")
    public String doFirst(){
        System.out.println("Handler method");
        return "success";
    }
}

  4.自己的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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--包扫描器-->
    <context:component-scan base-package="cn.dawn.day25interceptor"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day25/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--拦截器配置-->
    <mvc:interceptors>  <!--配置多个拦截器,多个拦截器链-->

        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.dawn.day25interceptor.FirstInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.dawn.day25interceptor.SecondInterceptor"></bean>
        </mvc:interceptor>

    </mvc:interceptors>

    <!--绑定注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

  此处的拦截器位置的前后,决定了他们执行的先后

  此处的/**表示任意多级目录的任意方法,全部的意思

  5.修改web.xml的中央调度器的上下文配置位置为上面那个xml

  6.jsp页面,我不是return了个success么,所以建一个success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Dawn
  Date: 2018/4/2
  Time: 14:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>成功</title>
</head>
<body>
    <h1>SUCCESS</h1>
</body>
</html>

  7.启动tomcat,访问处理器的那个处理方法/first

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8697266.html