spring过滤规则的名字含义_Spring MVC中各个filter的用法

本文介绍了Spring MVC中的两个过滤器:HiddenHttpMethodFilter和HttpPutFormContentFilter。HiddenHttpMethodFilter用于处理HTML表单中的PUT方法,而HttpPutFormContentFilter则直接获取PUT表单的值并传递给Controller。另外,文章还讲解了ShallowEtagHeaderFilter的工作原理,它是Spring提供的支持ETag的过滤器,用于判断资源是否需要更新。最后提到了RequestContextFilter和DelegatingFilterProxy,前者用于绑定请求到线程,后者是Filter与Spring Bean的桥梁。
摘要由CSDN通过智能技术生成

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

methodParam

_method_

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表单的代码必须如下:

......

配置如下:

httpPutFormcontentFilter

org.springframework.web.filter.HttpPutFormContentFilter

httpPutFormContentFilter

/*

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的源码如下:

protected voiddoFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException{

ShallowEtagResponseWrapper responseWrapper= new ShallowEtagResponseWrapper(response, null);

filterChain.doFilter(request, responseWrapper);//由此可知,服务器仍会处理请求

byte[] body =responseWrapper.toByteArray();int statusCode =responseWrapper.getStatusCode();if(isEligibleForEtag(request, responseWrapper, statusCode, body)) {

String responseETag=generateETagHeaderValue(body);

response.setHeader(HEADER_ETAG, responseETag);

String requestETag=request.getHeader(HEADER_IF_NONE_MATCH);if(responseETag.equals(requestETag)) {if (this.logger.isTraceEnabled()) {this.logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");

}

response.setStatus(304);

}else{if (this.logger.isTraceEnabled()) {this.logger.trace("ETag [" + responseETag + "] not equal to If-None-Match [" + requestETag + "], sending normal response");

}

copyBodyToResponse(body, response);

}

}else{if (this.logger.isTraceEnabled()) {this.logger.trace("Response with status code [" + statusCode + "] not eligible for ETag");

}

copyBodyToResponse(body, response);

}

}

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

配置ShallowEtagHeaderFilter的代码如下:

shallowEtagHeaderFilter

org.springframework.web.filter.ShallowEtagHeaderFilter

shallowEtagHeaderFilter

spring

RequestContextFilter

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

RequestContextFilter

org.springframework.web.filter.RequestContextFilter

RequestContextFilter

Faces Servlet

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中配置如下:

testFilter

org.springframework.web.filter.DelegatingFilterProxy

targetBeanName

spring-bean-name

contextAttribute

session

targetFilterLifecycle

false

testFilter

/*

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类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值