读书人

Spring with Tomcat 中文编码有关问题

发布时间: 2012-09-02 21:00:34 作者: rapoo

Spring with Tomcat 中文编码问题的解决方案

由于各种原因,Java 应用中有关中文编码的问题总是层出不穷,即使是在 i18n 发展已久的今日,为了能够在 Java 应用中良好地使用中文,程序员们仍然要花费许多额外的力气来调试、设置以解决有关中文编码的问题。针对在 Tomcat 服务器上运行的 Spring 应用,我总结了一些相关的经验,希望能帮助其他人在处理该问题时能少走一些弯路。以下条目纯属个人在开发中摸索出来的经验,并不一定适用于所有情况。在解决中文编码的问题时,也不一定需要将以下所罗列的条目逐一设置,往往只需要结合自己开发中的实际情况修改其中若干项目即可。

1. 修改 $CATALINA_HOME/conf/server.xml。
找到 Connector 的配置,添加 URIEncoding 属性,例如:

<Connector?URIEncoding="UTF-8"?port="8080"?protocol="HTTP/1.1"?????????? connectionTimeout="20000"?????????? redirectPort="8443"?/>

2. 修改 $CATALINA_HOME/bin/catalina.sh。(来源www.iocblog.net)
设置 CATALINA_OPTS 参数,如下:
JAVA_OPTS="$JAVA_OPTS?"-Djavax.servlet.request.encoding=UTF-8"?"-Dfile.encoding=UTF-8""

3. 修改 Web 应用中的 web.xml。
添加过滤器 CharacterEncodingFilter。如下:
Spring with Tomcat 中文编码有关问题的解决方案<filter>Spring with Tomcat 中文编码有关问题的解决方案????<filter-name>setCharacterEncoding</filter-name>Spring with Tomcat 中文编码有关问题的解决方案????<filter-class>Spring with Tomcat 中文编码有关问题的解决方案????????org.springframework.web.filter.CharacterEncodingFilterSpring with Tomcat 中文编码有关问题的解决方案????</filter-class>Spring with Tomcat 中文编码有关问题的解决方案????<init-param>Spring with Tomcat 中文编码有关问题的解决方案????????<param-name>encoding</param-name>Spring with Tomcat 中文编码有关问题的解决方案????????<param-value>UTF-8</param-value>Spring with Tomcat 中文编码有关问题的解决方案????</init-param>Spring with Tomcat 中文编码有关问题的解决方案</filter>Spring with Tomcat 中文编码有关问题的解决方案<!--?filtered?type?-->Spring with Tomcat 中文编码有关问题的解决方案<filter-mapping>Spring with Tomcat 中文编码有关问题的解决方案????<filter-name>setCharacterEncoding</filter-name>Spring with Tomcat 中文编码有关问题的解决方案????<url-pattern>*.do</url-pattern>Spring with Tomcat 中文编码有关问题的解决方案</filter-mapping>

4. 修改 Web 应用中的 app-servlet.xml。
找到 viewResolver bean 的配置,添加 contentType 属性,如下:
Spring with Tomcat 中文编码有关问题的解决方案<bean?id="viewResolver"Spring with Tomcat 中文编码有关问题的解决方案????class="org.springframework.web.servlet.view.InternalResourceViewResolver">Spring with Tomcat 中文编码有关问题的解决方案????<property?name="viewClass"Spring with Tomcat 中文编码有关问题的解决方案????????value="org.springframework.web.servlet.view.JstlView"?/>Spring with Tomcat 中文编码有关问题的解决方案????<property?name="prefix"?value="/"?/>Spring with Tomcat 中文编码有关问题的解决方案????<property?name="suffix"?value=".jsp"?/>Spring with Tomcat 中文编码有关问题的解决方案????<property?name="contentType">Spring with Tomcat 中文编码有关问题的解决方案????????<value>text/html;charset=UTF-8</value>Spring with Tomcat 中文编码有关问题的解决方案????</property>Spring with Tomcat 中文编码有关问题的解决方案</bean>

5. 在 JSP 页面中添加如下一行。
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"?pageEncoding="UTF-8"%>

6. 编译 war 包时需要注意指定编译器的编码。
例如,在 maven 的 pom.xml 中找到 maven-compiler-plugin 插件一节,指定 encoding:
Spring with Tomcat 中文编码有关问题的解决方案<plugin>Spring with Tomcat 中文编码有关问题的解决方案????<artifactId>maven-compiler-plugin</artifactId>Spring with Tomcat 中文编码有关问题的解决方案????<configuration>Spring with Tomcat 中文编码有关问题的解决方案????????<source>1.6</source>Spring with Tomcat 中文编码有关问题的解决方案????????<target>1.6</target>Spring with Tomcat 中文编码有关问题的解决方案????????<encoding>UTF-8</encoding>Spring with Tomcat 中文编码有关问题的解决方案????</configuration>Spring with Tomcat 中文编码有关问题的解决方案</plugin>

7. 在 Controller 中覆盖 handleRequestInternal 方法,指定 response 所采用的字符编码。
Spring with Tomcat 中文编码有关问题的解决方案@OverrideSpring with Tomcat 中文编码有关问题的解决方案protected?ModelAndView?handleRequestInternal(HttpServletRequest?request,Spring with Tomcat 中文编码有关问题的解决方案Spring with Tomcat 中文编码有关问题的解决方案????????HttpServletResponse?response)?throws?Exception?{Spring with Tomcat 中文编码有关问题的解决方案????response.setCharacterEncoding("UTF-8");Spring with Tomcat 中文编码有关问题的解决方案????return?super.handleRequestInternal(request,?response);Spring with Tomcat 中文编码有关问题的解决方案}?
...

读书人网 >软件架构设计

热点推荐