1)采用国际化。这样能解决页面的中文乱码问题。
a)把页面中所出现的中文写到属性文件中,例如:messages_zh.properties,
shipment.system = 出货小系统
shipment.jsnum = 条码
shipment.stylenum = 款式
b)native2ascii把文字转换成ascii码,
D:\project\shipment\web\WEB-INF\classes>native2ascii messages_zh.properties mess
ages_zh_CN.properties
c)页面统一采用utf-8编码
<%@ page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
d)在web.xml文件中引入JSTL与WEBWORK集成的类
<!-- Define the basename for a resource bundle for I18N -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>
<!-- 采用filter编码转换-->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.appfuse.web.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
e)filter类把请求的统一转换成utf-8,(在2.1.7版本以后自己处理好了)如下:
package org.appfuse.web;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-8-11
* Time: 13:17:00
* To change this template use File | Settings | File Templates.
*/
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 SetCharacterEncodingFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
/*
* Servlet编码
*/
request.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
}
public void destroy() {
}
}
e)在web-inf\classes中的webwork.properties文件(可能这个就能取代上面的,有待测试)中加入
webwork.custom.i18n.resources=messages
webwork.locale=zh_CN
webwork.i18n.encoding=GBK
f)在页面中还需添加JSTL标签,当然在classpath下要加入相关的类包
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
g)现在在页面中就可以采用jstl的EL来引入了
<fmt:message key="shipment.shipment"/>
2)把tomcate下面的conf文件夹下的server.xml文件中加入 URIEncoding="GBK",有了上面的可能这一个也是多余的,如:
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="GBK" />