spring mvc 乱码问题汇总

Life is short , play more!
本文来自lihao's Blog,转载请注明。

get,post请求乱码问题很是恼火。 不过解决的办法不外乎如下几个。 为了国际化,通常我们将字符集设为utf-8是最为方便。

1.  tomcat的配置。

<Connector port="8080" 加上后面这条设置 uRIEncoding="utf-8" />

 

2.  web.xml 中的编码filter设置

<filter>

<filter-name>encodingFilter</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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param>

</filter>

<filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern>

</filter-mapping>

 

3. jsp 页面的代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

 

4.  locale对应的默认编码设定

 <locale-encoding-mapping-list>
    <locale-encoding-mapping>
        <locale>en</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>cz</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
        <locale>de</locale>
        <encoding>UTF-8</encoding>
    </locale-encoding-mapping>
</locale-encoding-mapping-list>

 

5.  当你使用maven时,最好将如下也设置为utf-8

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          ...
          <encoding>UTF-8</encoding>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

发表评论

电子邮件地址不会被公开。 必填项已用*标注