过滤器filter统一字符编码(spring框架自带)

该过滤器的统一字符编码只针对post方式提交有效

1.web.xml配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- 扫描加载配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
classpath:spring/applicationContext-mvc.xml,
classpath:spring/applicationContext.xml,
classpath:spring/applicationContext-dataSource.xml
</param-value>
  </context-param>
  <!--log4j配置文件开始-->  
<context-param>  
   <param-name>log4jConfigLocation</param-name>  
   <param-value>classpath:log4j.properties</param-value>  
</context-param>  
<listener>  
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>
<!-- post方式提交 统一字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

猜你喜欢

转载自blog.csdn.net/fxbfxb111/article/details/80747108