spring mvc的org.springframework.web.filter包下的Java文件如下:


类的结构如下:

AbstractRequestLoggingFilter及其子类

AbstractRequestLoggingFilter类定义了两个方法beforeRequest和afterRequest分别用于设定过滤前后执行的操作,它有三个子类,分别是CommonsRequestLoggingFilter、ServletContextRequestLoggingFilter和Log4jNestedDiagnosticContextFilter,这三个子类分别实现了各自的beforeRequest和afterRequest。其中,CommonsRequestLoggingFilter在过滤前后分别打印出一段debug的信息;ServletContextRequestLoggingFilter在过滤前后分别向日志文件中写入一段日志信息,日志文件可由log4j.properties等指定;Log4jNestedDiagnosticContextFilter则将日志信息存储到NDC中,NDC采用了一个类似栈的机制来push和pot上下文信息,每一个线程都独立地储存上下文信息,比如说一个servlet就可以针对 每一个request创建对应的NDC,储存客户端地址等信息。

CharacterEncodingFilter

该过滤器是配置编码格式的,在web.xml中设置如下:

  1. <filter>
  2. <filter-name>springCharacterEncodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>forceEncoding</param-name>
  6. <param-value>true</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>encoding</param-name>
  10. <param-value>UTF-8</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>springCharacterEncodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

HiddenHttpMethodFilter

html中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。可以配置如下:

  1. <filter>
  2. <filter-name>HiddenHttpMethodFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  4. <init-param>
  5. <param-name>methodParam</param-name>
  6. <param-value>_method_</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>HiddenHttpMethodFilter</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

在页面的form表单中设置method为Post,并添加一个如下的隐藏域:

<input type="hidden" name="_method" value="put" />

查看HiddenHttpMethodFilter源码

  1. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  2. String paramValue = request.getParameter(methodParam);
  3. if("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
  4. String method = paramValue.toUpperCase(Locale.ENGLISH);
  5. HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
  6. filterChain.doFilter(wrapper, response);
  7. } else
  8. {
  9. filterChain.doFilter(request, response);
  10. }
  11. }

由源码可以看出,filter只对Post方法进行过滤,且需要添加参数名为_method的隐藏域,也可以设置其他参数名,比如想设置为_method_,可以在HiddenHttpMethodFilter配置类中设置初始化参数:

  1. <filter>
  2. <filter-name>HiddenHttpMethodFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  4. <init-param>
  5. <param-name>methodParam</param-name>
  6. <param-value>_method_</param-value>
  7. </init-param>
  8. </filter>

HttpPutFormContentFilter

由HiddenHttpMethodFilter可知,html中的form的method值只能为post或get,我们可以通过HiddenHttpMethodFilter获取put表单中的参数键值对,而在Spring3中获取put表单的参数键值对还有另一种方法,即使用HttpPutFormContentFilter过滤器。

HttpPutFormContentFilter过滤器的作为就是获取put表单的值,并将之传递到Controller中标注了method为RequestMethod.put的方法中。

与HiddenHttpMethodFilter不同,在form中不用添加参数名为_method的隐藏域,且method不必是post,直接写成put,但该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单,也就是说,在使用该过滤器时,form表单的代码必须如下:

<form action="" method="put" enctype="application/x-www-form-urlencoded">

......

</form>

配置如下:

  1. <filter>
  2. <filter-name>httpPutFormcontentFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>httpPutFormContentFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

ShallowEtagHeaderFilter

ShallowEtagHeaderFilter是spring提供的支持ETag的一个过滤器,所谓ETag是指被请求变量的实体值,是一个可以与Web资源关联的记号,而Web资源可以是一个Web页,也可以是JSON或XML文档,服务器单独负责判断记号是什么及其含义,并在HTTP响应头中将其传送到客户端,以下是服务器端返回的格式:

ETag:"50b1c1d4f775c61:df3"

客户端的查询更新格式是这样的:

If-None-Match : W / "50b1c1d4f775c61:df3"

如果ETag没改变,则返回状态304然后不返回,这也和Last-Modified一样。

ShallowEtagHeaderFilter会将JSP等的内容缓存,生成MD5的key,然后在response中作为Etage的header返回给客户端。下次客户端对相同的资源(或者说相同的url)发出请求时,客户端会将之前生成的key作为If-None-Match的值发送到server端。 Filter会客户端传来的值和服务器上的做比较,如果相同,则返回304;否则,将发送新的内容到客户端。

查看ShallowEtagHeaderFilter的源码如下:

  1. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException{
  2. ShallowEtagResponseWrapper responseWrapper = new ShallowEtagResponseWrapper(response, null);
  3. filterChain.doFilter(request, responseWrapper);
  4. // 由此可知,服务器仍会处理请求
  5. byte[] body = responseWrapper.toByteArray();
  6. int statusCode = responseWrapper.getStatusCode();
  7. if (isEligibleForEtag(request, responseWrapper, statusCode, body)) {
  8. String responseETag = generateETagHeaderValue(body);
  9. response.setHeader(HEADER_ETAG, responseETag);
  10. String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
  11. if (responseETag.equals(requestETag)) {
  12. if (this.logger.isTraceEnabled()) {
  13. this.logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
  14. }
  15. response.setStatus(304);
  16. }
  17. else {
  18. if (this.logger.isTraceEnabled()) {
  19. this.logger.trace("ETag [" + responseETag + "] not equal to If-None-Match [" + requestETag + "], sending normal response");
  20. }
  21. copyBodyToResponse(body, response);
  22. }
  23. }
  24. else {
  25. if (this.logger.isTraceEnabled()) {
  26. this.logger.trace("Response with status code [" + statusCode + "] not eligible for ETag");
  27. }
  28. copyBodyToResponse(body, response);
  29. }
  30. }

由源码可知,ShallowEtagHeaderFilter只能根据结果判断是否重新向客户端发送数据,并不会不处理请求,因此节省带宽,而不能提高服务器性能。

配置ShallowEtagHeaderFilter的代码如下:

  1. <filter>
  2. <filter-name>shallowEtagHeaderFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</fliter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>shallowEtagHeaderFilter</filter-name>
  7. <servlet-name>spring</servlet-name>
  8. </filter-mapping>

RequestContextFilter

这是在Spring2.0时添加的类,通过LocaleContextHolder和RequestContextHolder把Http request对象基于LocalThread绑定到请求提供服务的线程上。现在一般使用DispatcherServlet这个中央分发器。现在RequestContextFilter过滤器主要用于第三方的Servlet,如JSF的FacesServlet。在Spring2.5之前都是使用该过滤器配置。配置如下:

  1. <filter>
  2. <filter-name>RequestContextFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>RequestContextFilter</filter-name>
  7. <servlet-name>Faces Servlet</servlet-name>
  8. </filter-mapping>

DelegatingFilterProxy

该类其实并不能说是一个过滤器,它的原型是FilterToBeanProxy,即将Filter作为spring的bean,由spring来管理。该类提供了在web.xml和application context之间的联系。

Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed bean that implements the Filter interface.

有以下几个参数可以设置:

(1) contextAttribute,使用委派Bean的范围,其值必须从org.springframework.context.ApplicationContext.WebApplicationContext中取得,默认值是session;其他可选的有request、globalSession和application

(2) targetFilterLifecycle,是否调用Filter的init和destroy方法,默认为false。

(3)targetBeanName,被代理的过滤器的bean的名字,该bean的类必须实现Filter接口。

在web.xml中配置如下:

  1. <filter>
  2. <filter-name>testFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4. <init-param>
  5. <param-name>targetBeanName</param-name>
  6. <param-value>spring-bean-name</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>contextAttribute</param-name>
  10. <param-value>session</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>targetFilterLifecycle</param-name>
  14. <param-value>false</param-value>
  15. </init-param>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>testFilter</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>

testBean是被spring容器管理的对象,对象的类实现了Filter接口。或者可以不用配置这个参数,这样spring容器中所有实现了Filter接口的类都被代理,实际就是把Servlet容器中的filters同spring容器中的bean关联起来,方便spring进行管理。

如果不配置DelegatingFilterProxy,则由于filter比bean先加载,也就是spring会先加载filter指定的类到container中,这样filter中注入的spring bean就为null了。如果将filter中加入DelegatingFilterProxy类,"targetFilterLifecycle"指明作用于filter的所有生命周期。原理是,DelegatingFilterProxy类是一个代理类,所有的请求都会首先发到这个filter代理,然后再按照"filter-name"委派到spring中的这个bean。

此外,spring bean实现了Filter接口,但默认情况下,是由spring容器来管理其生命周期的(不是由tomcat这种服务器容器来管理)。如果设置"targetFilterLifecycle"为True,则spring来管理Filter.init()和Filter.destroy();若为false,则这两个方法失效。

在Spring Security中就是使用该类进行设置。即在web.xml中配置该过滤器,然后在spring security相关的配置中设置相应的过滤器bean。但是该类是spring-web包下的类,不属于Spring Security类。

JAVA Spring MVC中各个filter的用法的更多相关文章

  1. Spring MVC中各个filter的用法

    转载:http://blog.csdn.net/qyp1314/article/details/42023725 Spring MVC中各个filter的用法 2014-12-19 09:08 105 ...

  2. Spring MVC中Session的正确用法<转>

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  3. 【转】Spring MVC中Session的正确用法之我见

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  4. Spring MVC中Session的正确用法之我见

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  5. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  6. 转:Spring mvc中@RequestMapping 6个基本用法小结

    Spring mvc中@RequestMapping 6个基本用法小结 发表于3年前(2013-02-17 19:58)   阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏 赞3 4 ...

  7. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  8. Spring mvc中@RequestMapping 6个基本用法小结

    Spring mvc中@RequestMapping 6个基本用法小结 小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMa ...

  9. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

随机推荐

  1. Keep On Movin

    上回书说道不愿透露姓名的巨巨还剩下一个数组,这个数组记录了他学习c++ 语言的过程. 现在这个数组a里有一些字符,第i个字符的数量是a[i].巨巨想用这些字符来构造一些回文串好让他的程序通过编译. 他 ...

  2. python的继承顺序

    python的继承顺序 python 创建类时分为新式类和旧式类 class A: # 经典类 def __init__(self): pass # 新类,可以在这里加 __metaclass__ = ...

  3. vs2013 乱码问题

    单击一下代码框  然后点文件  有个高级保存选项  改成utf-8  就可以显示中文了

  4. (转)性能分析之-- JAVA Thread Dump 分析综述

    原文链接:http://blog.csdn.net/rachel_luo/article/details/8920596 最近在做性能测试,需要对线程堆栈进行分析,在网上收集了一些资料,学习完后,将相 ...

  5. SpringCloud初体验:一、Eureka 服务的注册与发现

    Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...

  6. Accessing data in Hadoop using dplyr and SQL

    If your primary objective is to query your data in Hadoop to browse, manipulate, and extract it into ...

  7. Bootstrap-CL:Well

    ylbtech-Bootstrap-CL:Well 1.返回顶部 1. Bootstrap Well Well 是一种会引起内容凹陷显示或插图效果的容器 <div>.为了创建 Well,只 ...

  8. Hive中创建结构体、数组以及map

    ROW FORMAT DELIMITED 分隔符设置开始语句 FIELDS TERMINATED BY:设置字段与字段之间的分隔符 COLLECTION ITEMS TERMINATED BY:设置一 ...

  9. [转]MongoDB随笔2:使用查询

    转自:http://www.cnblogs.com/yangecnu/archive/2011/07/16/2108450.html 一.通过查询获取数据 在深入讨论查询之前,首先来了解一下查询返回的 ...

  10. bootstrap的datetimepicker.js的结束时间大于开始时间,当前日期之前的代码

    感觉不错的代码,贴出来,以后接着用 <link href="__ROOT__static/css/bootstrap-datetimepicker.min.css " rel ...