浅谈springMVC拦截器和过滤器总结

 更新时间:2017年01月19日 11:30:23   作者:龚细军  
本篇文章主要介绍了springMVC拦截器和过滤器总结,可以用来对访问的url进行拦截处理,有兴趣的可以了解一下。

拦截器: 用来对访问的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 ,灵活

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Kotlin的空指针处理

    详解Kotlin的空指针处理

    这篇文章主要介绍了详解Kotlin的空指针处理的相关资料,需要的朋友可以参考下
    2017-06-06
  • SpringBoot分页的实现与long型id精度丢失问题的解决方案介绍

    SpringBoot分页的实现与long型id精度丢失问题的解决方案介绍

    在以后的开发中,当全局唯一id的生成策略生成很长的Long型数值id之后会超过JS对Long型数据处理的能力范围,可能发生精度丢失而造成后端方法失效,我们要学会解决。分页功能虽然简单但是非常重要,对于刚接触项目的人一定要重点注意
    2022-10-10
  • spring注解如何为bean指定InitMethod和DestroyMethod

    spring注解如何为bean指定InitMethod和DestroyMethod

    这篇文章主要介绍了spring注解如何为bean指定InitMethod和DestroyMethod,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java随机生成手机短信验证码的方法

    Java随机生成手机短信验证码的方法

    这篇文章主要介绍了Java随机生成手机短信验证码的方法,涉及Java数学运算计算随机数及字符串操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Java多线程读写锁ReentrantReadWriteLock类详解

    Java多线程读写锁ReentrantReadWriteLock类详解

    本文详细讲解了Java多线程读写锁ReentrantReadWriteLock类,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • 通过AOP环绕通知如何实现事务控制

    通过AOP环绕通知如何实现事务控制

    这篇文章主要介绍了通过AOP环绕通知如何实现事务控制的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 基于HTTP协议实现简单RPC框架的方法详解

    基于HTTP协议实现简单RPC框架的方法详解

    RPC全名(Remote Procedure Call),翻译过来就是远程过程调用,本文将为大家介绍如何基于HTTP协议实现简单RPC框架,感兴趣的小伙伴可以了解一下
    2023-06-06
  • 详解Java的MyBatis框架与Spring框架整合中的映射器注入

    详解Java的MyBatis框架与Spring框架整合中的映射器注入

    映射器注入方式可以将MyBatis与Spring映射好的XML文件实现配置共用,这里我们就来详解Java的MyBatis框架与Spring框架整合中的映射器注入:
    2016-06-06
  • spring boot2.0实现优雅停机的方法

    spring boot2.0实现优雅停机的方法

    这篇文章主要介绍了spring boot2.0实现优雅停机的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Java的Servlet及其生命周期详解

    Java的Servlet及其生命周期详解

    这篇文章主要介绍了Java的Servlet及其生命周期详解,Servlet是用Java编写的服务器端程序,一门用于开发动态web资源的技术,其主要功能在与交互式的浏览和修改数据,生成动态web内容,需要的朋友可以参考下
    2023-11-11

最新评论