SpringMVC--拦截器

标签: SpringMVC拦截器知识 SpringMVC高级知识 SpringMVC知识点 SpringMVC基础知识点 SpringMVC拦截器
22人阅读 评论(0) 收藏 举报
分类:

拦截器

1.   自定义拦截器概述

l  Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口

preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false

postHandle():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。

afterCompletion():这个方法DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。

2.   代码(单个拦截器)

①   自定义拦截器类

package com.xypuxing.springmvc.interceptors;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

 

public class FirstHandlerInterceptor implements HandlerInterceptor {

 

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {

System.out.println(this.getClass().getName() + " - afterCompletion");

}

 

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(this.getClass().getName() + " - postHandle");

}

 

@Override

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2) throws Exception {

System.out.println(this.getClass().getName() + " - preHandle");

return true;

}

②     配置拦截器

<mvc:interceptors>

<!-- 声明自定义拦截器 -->

<bean id="firstHandlerInterceptor"

      class="com.atguigu.springmvc.interceptors.FirstHandlerInterceptor"></bean>

</mvc:interceptors>


③     断点调试拦截器执行流程


3.   代码(多个拦截器)

①   自定义拦截器类(两个)

package com.xypuxing.springmvc.interceptors;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

 

public class FirstHandlerInterceptor implements HandlerInterceptor {

 

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println(this.getClass().getName() + " - afterCompletion");

}

 

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(this.getClass().getName() + " - postHandle");

}

 

@Override

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2) throws Exception {

System.out.println(this.getClass().getName() + " - preHandle");

return true;

}

 

}

package com.atguigu.springmvc.interceptors;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

 

public class SecondHandlerInterceptor implements HandlerInterceptor {

 

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println(this.getClass().getName() + " - afterCompletion");

}

 

@Override

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(this.getClass().getName() + " - postHandle");

}

 

@Override

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2) throws Exception {

System.out.println(this.getClass().getName() + " - preHandle");

return true;

}

 

}

②     配置自定义拦截器

<mvc:interceptors>

<!-- 声明自定义拦截器 -->

<bean id="firstHandlerInterceptor"

  class="com.atguigu.springmvc.interceptors.FirstHandlerInterceptor"></bean>

<!-- 配置拦截器引用 -->

<mvc:interceptor>                        

<mvc:mapping path="/empList"/>

<!-- <mvc:exclude-mapping path="/empList"/> -->

<bean id="secondHandlerInterceptor"

         class="com.atguigu.springmvc.interceptors.SecondHandlerInterceptor"></bean>

</mvc:interceptor>

</mvc:interceptors>

两个都是返回true :

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - preHandle

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - preHandle

************************************biz method*******************************

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - postHandle

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - postHandle

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - afterCompletion

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - afterCompletion

两个都是返回false:

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - preHandle

true,false

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - preHandle

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - preHandle

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - afterCompletion

false,true

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - preHandle

 

4.   多个拦截方法的执行顺序

①     关于执行顺序

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - preHandle

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor – preHandle

************************************biz method*******************************

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - postHandle

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - postHandle

com.xypuxing.springmvc.interceptors.SecondHandlerInterceptor - afterCompletion

com.xypuxing.springmvc.interceptors.FirstHandlerInterceptor - afterCompletion

②     执行顺序图解

 

③     从源代码的执行角度分析流程:

boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {

if (getInterceptors() != null) {

for (int i = 0; i < getInterceptors().length; i++) {

HandlerInterceptor interceptor = getInterceptors()[i];

if (!interceptor.preHandle(request, response, this.handler)) {

triggerAfterCompletion(request, response, null);

return false;

}

this.interceptorIndex = i;

}

}

return true;

}

void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {

if (getInterceptors() == null) {

return;

}

for (int i = getInterceptors().length - 1; i >= 0; i--) {

HandlerInterceptor interceptor = getInterceptors()[i];

interceptor.postHandle(request, response, this.handler, mv);

}

}

void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)

throws Exception {

 

if (getInterceptors() == null) {

return;

}

for (int i = this.interceptorIndex; i >= 0; i--) {

HandlerInterceptor interceptor = getInterceptors()[i];

try {

interceptor.afterCompletion(request, response, this.handler, ex);

}

catch (Throwable ex2) {

logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);

}

}

}

④     源码分析:分析interceptorIndex的值情况


 

查看评论

springMVC--拦截器

拦截器 定义 l  Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理 局部拦截器 l  针对某个HandlerM...
  • CSDN_GIA
  • CSDN_GIA
  • 2017-02-13 10:08:38
  • 173

SpringMVC参数传递方式

1、通过@RequestParam注解 例: @RequestMapping(value = "/update") public String update( @RequestParam(...
  • maple980326
  • maple980326
  • 2016-07-06 10:43:26
  • 271

struts2拦截器

  • 2017年10月18日 16:09
  • 8.26MB
  • 下载

拦截器下载

  • 2018年01月12日 10:21
  • 29KB
  • 下载

springMVC--页面缓存

页面缓存 简单理解缓存原理 互联网架构 页面缓存 使用Oscache实现页面缓存。 测试页面缓存 创建web工程,导入jar ...
  • CSDN_GIA
  • CSDN_GIA
  • 2017-02-13 10:04:21
  • 1001

Springmvc--上传文件

SpringMVC为文件上传提供了直接支持,通过MultipartResolver实现。默认没有装配MultipartResolver,需要先在上下文中配置MultipartResolver。1、需要...
  • u014785687
  • u014785687
  • 2017-09-10 11:01:01
  • 63

过滤器和拦截器区别区别

  • 2011年07月27日 16:19
  • 29KB
  • 下载

SpringMVC--入门

MVC是一种理念,一种思想,各种语言都有对这种思想的实现框架,.NET有,Java也有,而SpringMVC就是实现了MVC思想的Web框架.而各种框架存在的意思就是帮助程序员更好的管理,控制项目,更...
  • bemavery
  • bemavery
  • 2015-04-10 21:26:49
  • 9160

springMVC--控制器

控制器 一、控制器架构图 二、Controller简介 1、收集、验证请求参数并绑定到命令对象; 2、将命令对象交给业务对象,由业务对象处理并返回模型数据; 3、返回ModelAndView...
  • CSDN_GIA
  • CSDN_GIA
  • 2017-02-10 18:38:02
  • 373

springmvc--文件上传

说一下利用springmvc框架弄文件上传到服务器上: 1.首先就是导入jar包(依赖): dependency> groupId>junitgroupId> artifact...
  • desert568
  • desert568
  • 2018-01-13 15:06:56
  • 27
    个人资料
    持之以恒
    等级:
    访问量: 8454
    积分: 1000
    排名: 5万+
    文章存档
    最新评论