晕了一上午碰到了从JSP页面请求到ACTION中出现了中文问题。找了很久也不知道到底问题出现在那,郁闷呀。总觉得很奇怪,因为我是原来的系统(中文传递没有问题)中加入一些页面,也添加了一个Action,可它有中文问题了。想了很久也不能解决,真是呀。最后,我干脆把原来的系统还原,然后重新在系统中添加所要修改的东东,居然没有问题了。
总结一下原系统解决乱码的方案:
1)采用国际化。这样能解决页面的中文乱码问题。
a)把页面中所出现的中文写到属性文件中,例如:messages_zh.properties,
b)native2ascii把文字转换成ascii码,
c)页面统一采用utf-8编码
d)在web.xml文件中引入JSTL与WEBWORK集成的类
e)filter类把请求的统一转换成utf-8,(在2.1.7版本以后自己处理好了)如下:
ee)在web-inf\classes中的webwork.properties文件(可能这个就能取代上面的,有待测试)中加入
f)在页面中还需添加JSTL标签,当然在classpath下要加入相关的类包
g )现在在页面中就可以采用jstl的EL来引入了
2 )把tomcate下面的conf文件夹下的server.xml文件中加入 URIEncoding="GBK",有了上面的可能这一个也是多余的,如:
现在想想,当时可能我太性急,有点慌,盲目,所以没有注意到原来我在系统中加入了filter来转换编码(utf-8),而在我新添加的页面中没有注意到这一点,所以怎么处理还是有乱码。所以在以后的开发当中一定要沉着冷静专注,要弄清其所以然,才能顺利的解决好问题。
总结一下原系统解决乱码的方案:
1)采用国际化。这样能解决页面的中文乱码问题。
a)把页面中所出现的中文写到属性文件中,例如:messages_zh.properties,
shipment.system
=
出货小系统
shipment.jsnum = 条码
shipment.stylenum = 款式
shipment.jsnum = 条码
shipment.stylenum = 款式
b)native2ascii把文字转换成ascii码,
D:\project\shipment\web\WEB
-
INF\classes
>
native2ascii messages_zh.properties mess
ages_zh_CN.properties
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">
<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 >
< 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() {
}
}
/**/ /**
* 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() {
}
}
ee)在web-inf\classes中的webwork.properties文件(可能这个就能取代上面的,有待测试)中加入
webwork.custom.i18n.resources
=
messages
webwork.locale = zh_CN
webwork.i18n.encoding = GBK
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" />
port ="8080" maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
enableLookups ="false" redirectPort ="8443" acceptCount ="100"
debug ="0" connectionTimeout ="20000"
disableUploadTimeout ="true" URIEncoding ="GBK" />
现在想想,当时可能我太性急,有点慌,盲目,所以没有注意到原来我在系统中加入了filter来转换编码(utf-8),而在我新添加的页面中没有注意到这一点,所以怎么处理还是有乱码。所以在以后的开发当中一定要沉着冷静专注,要弄清其所以然,才能顺利的解决好问题。