拦截器: 用来对访问的url进行拦截处理

用处: 权限验证,乱码设置等

spring-mvc.xml文件中的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
">
    <!--编写拦截器-->
<mvc:interceptors>
<!--对特定的url拦截-->
<mvc:interceptor>
<mvc:mapping path="/test.do"/>
<bean class="com.hbut.interceptor.TestInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<!--对特定的模块拦截第一级别拦截-->
<mvc:mapping path="/test/×/"/>
<bean class="com.hbut.interceptor.TestInterceptor1"/>
</mvc:interceptor>
        <mvc:interceptor>
<!--对特定的模块拦截-->
<mvc:mapping path="/test/×"/>
<bean class="com.hbut.interceptor.TestInterceptor2"/>
</mvc:interceptor>
    </mvc:interceptors>

对所有的url进行拦截

  <mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>

java代码

package com.hbut.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @Author XiJun.Gong
* @DATE 2016/6/1.
* aim: com.hbut.interceptor
*/
public class TestInterceptor implements HandlerInterceptor { @Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

//todo 在此处添加要操作code
System.out.println("preHandle");
return true; //todo 此处为false时,请求不会到达control层
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle"); //todo 可以用来修改信息,跳转等
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion"); //todo 最后执行
}
}

另一种拦截器:大同小异

package com.hbut.interceptor;

import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor; /**
* @Author XiJun.Gong
* @DATE 2016/6/1.
* aim: com.hbut.interceptor
*/
public class Test2Interceptor implements WebRequestInterceptor {
@Override
public void preHandle(WebRequest webRequest) throws Exception {
} @Override
public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { } @Override
public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { }
}

过滤器: 依赖于servlet容器,使用回调函数,过滤范围大

拦截器: 依赖于框架容器 比如spring、mybatis ,灵活

参考文章: 1. http://blog.sina.com.cn/s/blog_6a0cd5e501012bt9.html

2.  http://haohaoxuexi.iteye.com/blog/1750680

springMVC拦截器和过滤器总结的更多相关文章

  1. SpringMVC拦截器和过滤器

  2. 面试题:struts 拦截器和过滤器

    拦截器和过滤器的区别 过滤器是servlet规范中的一部分,任何java web工程都可以使用. 拦截器是struts2框架自己的,只有使用了struts2框架的工程才能用. 过滤器在url-patt ...

  3. SpringMVC的拦截器和过滤器的区别

    一 简介 (1)过滤器: 依赖于servlet容器.在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次.使用过滤器的目的是用来做一些过滤操作,获取我们 ...

  4. SpringMVC学习笔记:拦截器和过滤器

    首先说明一下二者的区别: 1. 拦截器基于java的反射机制,而过滤器是基于函数回调 2. 拦截器不依赖于servlet容器,过滤器依赖servlet容器 3. 拦截器只能对action请求起作用,而 ...

  5. Spring拦截器和过滤器

    什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...

  6. Spring Boot2(七):拦截器和过滤器

    一.前言 过滤器和拦截器两者都具有AOP的切面思想,关于aop切面,可以看上一篇文章.过滤器filter和拦截器interceptor都属于面向切面编程的具体实现. 二.过滤器 过滤器工作原理 从上图 ...

  7. java 拦截器和过滤器区别(转载)

    1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几乎所有的 ...

  8. struts2拦截器和过滤器区别

    1.拦截器是基于java反射机制的,而过滤器是基于函数回调的.2.过滤器依赖于servlet容器,而拦截器不依赖于servlet容器.3.拦截器只能对Action请求起作用,而过滤器则可以对几乎所有请 ...

  9. java 中的拦截器和过滤器

    区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几 ...

随机推荐

  1. Web Compiler

    Web Compiler The easiest and most powerful way to compile LESS, Scss, Stylus, JSX and CoffeeScript f ...

  2. Linux时间戳和标准时间的互转

      转http://hi.baidu.com/taolizao/blog/item/2d6f9a1ba50ef3eae0fe0ba9.html 在LINUX系统中,有许多场合都使用时间戳的方式表示时间 ...

  3. Sql Server 中一个非常强大的日期格式化函数

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0)-- 05 16 2006 10:57AMSelect CONV ...

  4. android学习计划2

    1.linux下Kconfig编写规范 2.linux下Makefile编写规范 3.android下Makefile编写规范 4.android.mk编写规范 5.android系统裁剪

  5. JMeter入门使用指南

    初识JMeter 最近开始接触和使用JMeter进行性能测试,也是因为工作需要,不得不学习更多新技能,在此之前一直使用LR进行WEB系统的压力测试,但是在ZK开发的WEB系统,我选择使用JMeter. ...

  6. Javascript > Eclipse > problems encountered during text search

    Reproduce: Ctrl + H, Select "File Search", will encounter eclipse kinds of bug/error alert ...

  7. webdriver杀死浏览器和Chromedriver进程

    /**     * 执行dos命令     * @param command     */    public static void command(String command) {       ...

  8. [转]VB Winsock 控件TCP与UDP连接实例

    [-] 可能的用途 选择通讯协议 协议的设置 确定计算机的名称 TCP 连接初步 接受多个连接请求 UDP 初步 关于 Bind 方法   利用 WinSock 控件可以与远程计算机建立连接,并通过用 ...

  9. 中文 iOS/Mac 开发博客列表(转)

    转自https://github.com/tangqiaoboy/iOSBlogCN 中文 iOS/Mac 开发博客列表 本博客列表会不断更新维护,如果有推荐的博客,请到此处提交博客信息. 本博客列表 ...

  10. 特殊符号 && 和 ||

    一.值为false的情况 如果逻辑对象值为0,-0, null,undefined,false,"",NaN.那么值为false. 二.&& || 的 理解 1.& ...