SpringMVC拦截器

x33g5p2x  于2021-03-14 发布在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(280)

拦截器:是指通过统一拦截从浏览器发往服务器的请求来完成功能的增强。
使用场景:解决请求的共性问题(如:乱码问题、权限验证问题等)
1.首先编写拦截器类实现HandlerInterceptor接口

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

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

public class Test1Interceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("afterCompletion执行了");
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("postHandle执行了");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		System.out.println("preHandle执行了");
		return true;
	}
	
}

2.将拦截器注册进SpringMVC框架中
首先要在beans中添加 xmlns:mvc="http://www.springframework.org/schema/mvc"
以及在xsi:schemaLocation="“中添加
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

<?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:tx="http://www.springframework.org/schema/tx"
     xmlns:task="http://www.springframework.org/schema/task"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/task
		 http://www.springframework.org/schema/task/spring-task-3.1.xsd
		 http://www.springframework.org/schema/mvc
		 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">	
	<!-- 注册拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
		<!-- 设置拦截条件,这里拦截viewAll.form的请求 -->
		<mvc:mapping path="/viewAll.form"/>
			<bean class="com.test.interceptor.Test1Interceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
</beans>

拦截器三个方法的介绍
1.preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2)方法,在请求被处理之前进行调用
返回值:表示是否要将当前的请求拦截下来,如果是true则拦截下来,false则不会;HttpServletRequest存储所有请求的内容,HttpServletResponse存储所有响应的内容;Object arg2表示的是被拦截的请求目标对象。

2.postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3)方法,在请求被处理之后进行调用,可以通过ModelAndView参数来改变显示的视图,或修改发往视图的方法。

3.afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)方法,在请求结束之后才进行调用,这个方法一般用来进行销毁一般很少被使用。

多个拦截器执行顺序的示意图

通过实现WebRequestInterceptor接口实现的拦截器(一般很少使用)

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;

public class Test2Interceptor implements WebRequestInterceptor{

	@Override
	public void afterCompletion(WebRequest arg0, Exception arg1)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(WebRequest arg0, ModelMap arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void preHandle(WebRequest arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}
}

拦截器使用场景
使用原则:处理所有请求的共同问题
1.解决乱码问题
2.解决权限问题

拦截器和过滤器
①拦截器是基于java的反射机制的,而过滤器是基于函数回调。
②拦截器不依赖与servlet容器,过滤器依赖与servlet容器。
③拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用。
④拦截器可以访问action上下文、值栈里的对象,而过滤器不能访问。
⑤在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次。
⑥拦截器可以获取IOC容器中的各个bean,而过滤器就不行,这点很重要,在拦截器里注入一个service,可以调用业务逻辑

上一篇:版本控制工具

相关文章