在Spring整合Filter的时候我们需要用到Servlet,下面是Spring整合Filter的过程和前面一张的Spring整合Servlet是一起的。
犹豫Spring已经给我们提供了一个过滤器的代理类所以在这里我们不需要象整合Servlet一样去写自己的代理类
org.springframework.web.filter.DelegatingFilterProxy这个是Spring给我们提供的一个Filter代理所有的Filter都要经过这个代理类
第一步,配置web.xml文件在xml文件中注册我们自己的Filter
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Filter -->//这里是我们刚刚添加的filter
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Servlet -->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.xwl.estore.servlet.ServletToBeanProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第二步、编写我们自己的Filter必须要实现Filter接口
package com.xwl.estore.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter{
private String encoding;
private String contentType;
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setContentType(contentType);
System.out.println(encoding+" "+contentType);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
第三步、在Spring容器中注册我们自己写的Bean
这里我们不用anntation而是用xml的形式,原因是方便我们注入属性
在filter中添加你想要冲外界得到的值,就和我们平时的initParameter是一样的
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.xwl.estore"></context:component-scan>
<!-- Filter -->
<bean id="encodingFilter" class="com.xwl.estore.filter.EncodingFilter">
<property name="contentType">
<value>text/html;charset=utf-8</value>
</property>
<property name="encoding">
<value>utf-8</value>
</property>
</bean>
</beans>
第四步、进行测试
在浏览器中请求任何的url 看后台有没有输出你诸如的属性的值是否是对的,对了说明成功了!