springmvc · 2019-09-10 0

SpringMVC拦截器及WebMvcConfigurer配置拦截器

一、实现接口

接口 HandlerInterceptor 有:

preHandle():在目标方法运行之前调用;return true放行,return false不放行
postHandle():在目标方法运行之后调用
afterCompletion():在请求整个完成之后;来到目标页面之后调用

拦截器 MyFirstInterceptor

public class MyFirstInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyFirstInterceptor preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyFirstInterceptor postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyFirstInterceptor afterCompletion");
    }
}

拦截器 MySecondInterceptor

public class MySecondInterceptor  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MySecondInterceptor preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MySecondInterceptor postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MySecondInterceptor afterCompletion");
    }
}

二、配置拦截器

<?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: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">

    <!--扫描所有组件-->
    <context:component-scan base-package="com.example"/>

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <mvc:interceptors>
        <!--配置某个拦截器,默认是拦截所有请求的-->
        <bean class="com.example.interceptor.MyFirstInterceptor"/>
        <!--配置某个拦截器更详细的信息-->
        <mvc:interceptor>
            <mvc:mapping path="/handle01"/>
            <bean class="com.example.interceptor.MySecondInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

三、测试

@Controller
public class HelloController {

    @RequestMapping("/handle01")
    public String handle01(Map<String, Object> map){
        System.out.println("handle01");
        return "success.jsp";
    }
}

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        System.out.println("success.jsp");
    %>
    <h4>success</h4>
</body>
</html>

进行访问:

http://localhost:8080/handle01

结果:

MyFirstInterceptor preHandle
MySecondInterceptor preHandle
handle01
MySecondInterceptor postHandle
MyFirstInterceptor postHandle
success.jsp
MySecondInterceptor afterCompletion
MyFirstInterceptor afterCompletion

四、拦截流程

1.正常拦截流程:

preHandle、目标方法、postHandle、页面、拦截器afterCompletion

拦截器的preHandle:是按照顺序执行;拦截器的postandle:是按照逆序执行;拦截器的afterCompletion:是按照逆序执行

2.其他拦截流程:

只要preHandler不放行就没有以后的流程;只要放行,afterCompletion都会执行

栗:MyFirstInterceptor放行,MySecondInterceptor不放行

MyFirstInterceptor preHandle
MySecondInterceptor preHandle
MyFirstInterceptor afterCompletion

五、WebMvcConfigurer 配置拦截器

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Override
    public  void addInterceptors(InterceptorRegistry registry) {
        MyFirstInterceptor first = new MyFirstInterceptor();
        MySecondInterceptor second = new MySecondInterceptor();
        registry.addInterceptor(first).addPathPatterns("/**");
        registry.addInterceptor(second).addPathPatterns("/handle01");
    }
}

六、springmvc 拦截器与 servlet 过滤器区别

  1. 过滤器实现 Filter 接口对象,拦截器是实现 HandleInterceptor
  2. 过滤器是在拦截器之前执行的
  3. 过滤器是 tomcat 服务器创建的对象,拦截器是 springmvc 容器创建的对象
  4. 过滤器是一个执行时间点;拦截器是三个执行时间点
  5. 过滤器可以处理jsp、js、html等;拦截器是侧重拦截Controller的对象,如果你的请求不能被DispatcherServlet接收,这个请求不会执行拦截器的内容
  6. 拦截器拦截普通类方法执行,过滤器过滤servlet请求响应