SpringMVC框架05——拦截器

1、拦截器概述

Spring MVC的拦截器(Interceptor)与Java Servlet的过滤器(Filter)类似,它主要用于拦截用户的请求并做相应的处理,通常应用在权限验证、记录请求信息的日志、判断用户是否登录等功能上。

2、拦截器与过滤器的区别

拦截器是使用JDK动态代理实现的,拦截的是对应调用方法的拦截;
过滤器是使用Filter实现的,拦截的是request请求对象

3、定义单个拦截器

创建Controller类,代码如下:

@Controller
public class InterceptorController {
    @RequestMapping("/gotoTest")
    public String gotoTest(Model model){
        System.out.println("测试拦截器,控制器中gotoTest()方法执行。。。。");
        model.addAttribute("mess","拦截器测试!");
        return "test";
    }
}

创建拦截器类,代码如下:

public class TestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("preHandle()......");
        return true;
    }

    @Override
    public void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) 
throws Exception { System.out.println("postHandle()......"); } @Override public void afterCompletion(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, Exception e)
throws Exception { System.out.println("afterCompletion()......"); } }

 

配置springmvc.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: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-4.3.xsd">

    <!--配置扫描控制器类-->
    <context:component-scan base-package="com.interceptor.controller"></context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--配置拦截路径-->
            <mvc:mapping path="/**"/>
            <!--不需要拦截的路径-->
            <mvc:exclude-mapping path="/login" />
            <bean class="com.demo.controller.TestExceptionController"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

创建视图文件test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>测试拦截器</title>
</head>
<body>
    <p>视图:</p>
    <p>内容:${mess}</p>
</body>
</html>

测试结果,页面展示:

控制台输出结果:

在拦截器类TestInterceptor中实现了HandlerInterceptor接口,并重写了接口中的3个方法:

  • preHandle()方法:该方法在控制器的处理请求方法前执行,其返回值表示是否中断后续操作,返回true表示继续向下执行,返回false表示中断后续操作;
  • postHandle()方法:该方法在控制器的处理请求方法调用之后、解析视图之前执行,可以通过此方法对请求域中的模型和视图做进一步的修改;
  • afterCompletion()方法:该方法在控制器的处理请求方法执行完成后执行,即视图渲染结束后执行,可以通过此方法实现一些资源清理、记录日志信息等工作。

4、配置多个拦截器

创建Controller控制器类,代码如下:

@Controller
public class InterceptorController {
    @RequestMapping("/gotoTest")
    public String gotoTest(Model model){
        System.out.println("测试拦截器,控制器中gotoTest()方法执行。。。。");
        model.addAttribute("mess","拦截器测试!");
        return "test";
    }
}

创建多个拦截器类,
TestInterceptor1.java

public class TestInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("interceptor1......preHandle()......");
        return true;
    }

    @Override
    public void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) 
throws Exception { System.out.println("interceptor1......postHandle()......"); } @Override public void afterCompletion(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, Exception e)
throws Exception { System.out.println("interceptor1......afterCompletion()......"); } }

 

TestInterceptor2.java

public class TestInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("interceptor2...preHandle().......");
        return true;
    }

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

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

在springmvc.xml中配置多个拦截器

<!--配置拦截器-->
<mvc:interceptors>
    <!--配置拦截器1-->
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.interceptor.controller.TestInterceptor1"></bean>
    </mvc:interceptor>
    <!--配置拦截器2-->
    <mvc:interceptor>
        <mvc:mapping path="/gotoTest"/>
        <bean class="com.interceptor.controller.TestInterceptor2"></bean>
    </mvc:interceptor>
</mvc:interceptors>

测试结果,页面显示结果:

控制台显示结果:

在Web应用中通常需要配置多个拦截器同时工作,这时它们的preHandle方法按照配置文件中拦截器的配置顺序执行,而它们的postHandle方法和afterCompletion方法则按照配置顺序的反序执行。

posted @ 2019-03-15 23:04  Web1024  阅读(417)  评论(0编辑  收藏  举报