fmt:message 中文乱码问题

1)采用国际化。这样能解决页面的中文乱码问题。

a)把页面中所出现的中文写到属性文件中,例如:messages_zh.properties,
html

[html] view plain copy
  1. shipment.system = 出货小系统  java

  2. shipment.jsnum = 条码  web

  3. shipment.stylenum = 款式  tomcat

b)native2ascii把文字转换成ascii码,
D:\project\shipment\web\WEB-INF\classes>native2ascii messages_zh.properties mess
ages_zh_CN.properties

c)页面统一采用utf-8编码
[html] view plain copy
  1. <%@ page pageEncoding="utf-8"contentType="text/html;charset=utf-8" %>app

  2. <metahttp-equiv="Content-Type"content="text/html; charset=utf-8">jsp

d)在web.xml文件中引入JSTL与WEBWORK集成的类
[html] view plain copy
  1. <!-- Define the basename for a resource bundle for I18N -->ide

  2. <context-param>oop

  3. <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>测试

  4. <param-value>messages</param-value>ui

  5. </context-param>

[html] view plain copy
  1. <!-- 采用filter编码转换-->

  2. <filter>

  3. <filter-name>Set Character Encoding</filter-name>

  4. <filter-class>org.appfuse.web.SetCharacterEncodingFilter</filter-class>

  5. </filter>

  6. <filter-mapping>

  7. <filter-name>Set Character Encoding</filter-name>

  8. <url-pattern>/*</url-pattern>

  9. </filter-mapping>


e)filter类把请求的统一转换成utf-8,(在2.1.7版本之后本身处理好了)以下:
[java] view plain copy
  1. package org.appfuse.web;  

  2. /**

  3. * Created by IntelliJ IDEA.

  4. * User: Administrator

  5. * Date: 2005-8-11

  6. * Time: 13:17:00

  7. * To change this template use File | Settings | File Templates.

  8. */

  9. import java.io.IOException;  

  10. import javax.servlet.Filter;  

  11. import javax.servlet.FilterChain;  

  12. import javax.servlet.FilterConfig;  

  13. import javax.servlet.ServletException;  

  14. import javax.servlet.ServletRequest;  

  15. import javax.servlet.ServletResponse;  

  16. publicclass SetCharacterEncodingFilter implements Filter{  

  17. publicvoid init(FilterConfig arg0) throws ServletException {  

  18. }  

  19. publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  

  20. /*

  21. * Servlet编码

  22. */

  23. request.setCharacterEncoding("utf-8");  

  24. chain.doFilter(request,response);  

  25. }  

  26. publicvoid destroy() {  

  27. }  

  28. }  


e)在web-inf\classes中的webwork.properties文件(可能这个就能取代上面的,有待测试)中加入
[html] view plain copy
  1. webwork.custom.i18n.resources=messages

  2. webwork.locale=zh_CN

  3. webwork.i18n.encoding=GBK


f)在页面中还需添加JSTL标签,固然在classpath下要加入相关的类包
[html] view plain copy
  1. <%@ taglib uri="http://java.sun.com/jstl/fmt_rt"prefix="fmt" %><spanstyle="font-family: Arial, Helvetica, sans-serif;"></span>

g)如今在页面中就能够采用jstl的EL来引入了
[html] view plain copy
  1. <fmt:messagekey="shipment.shipment"/>

2)把tomcate下面的conf文件夹下的server.xml文件中加入 URIEncoding="GBK",有了上面的可能这一个也是多余的,如:
[html] view plain copy
  1. <Connector

  2. port="8080"maxThreads="150"minSpareThreads="25"maxSpareThreads="75"

  3. enableLookups="false"redirectPort="8443"acceptCount="100"

  4. debug="0"connectionTimeout="20000"

  5. disableUploadTimeout="true"URIEncoding="GBK"/>