中文存到数据库中总是显示乱码。DEBUG发现页面中文到Service都是正确的中文。试着在数据库中把字段值改为中文(字段类型为Nvarchar),结果页面上可以正确显示,说明页面设置正确。 问题出在数据库层。
后来检查数据库连接字段,发现一个参数
sendStringParametersAsUnicode=false
将其改为 sendStringParametersAsUnicode=true 问题解决。
查MSDN资料
如果 sendStringParametersAsUnicode 属性设置为“true”,则字符串参数将以 Unicode 格式发送给服务器。
如果 sendStringParametersAsUnicode 属性设置为“false”,则字符串参数将以非 Unicode 格式(例如 ASCII/MBCS 而不是 Unicode)发送给服务器。
sendStringParametersAsUnicode 属性的默认值为“true”。
注意:
2. 在struts.properties 添加:
struts.devMode=false struts.enable.DynamicMethodInvocation=true struts.i18n.reload=true struts.ui.theme=simple
struts.locale=zh_CN struts.i18n.encoding=UTF-8
struts.serve.static.browserCache=false struts.url.includeParams=none
其中locale、encoding就是字符集的设定了。
3. 在web.xml加个filter
<!-- zh-cn encoding --> <filter> <filter-name>struts-cleanup </filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
跟上述方法,类似还有在action中设定字符编符.
HttpServletResponse response = null; response = ServletActionContext.getResponse(); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
通过上述方法,基本就可以搞定中文乱码的问题了。当然,也有例外(如web server的版本/数据库的版本等等)。象在我的一个项目碰到一个中文乱码,tomcate5.5是会乱码的,而在tomcate6中就不会。这边就涉及到tomcate connector字符的设置了。
<Connector port="80" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
--------------------------------------------------------------------
后记之一:在使用struts2时,仍是遇到一种乱码。后来调试才发现,struts2的web.xml配置是有顺序的。
在web.xml中EncodingFilter的位置应该在Struts2的FilterDispatcher之前,因为要先调整字符集,然后进入Action。
按照Struts2的API,filter的顺序是 struts-cleanup filter SiteMesh filter FilterDispatcher
--------------------------------------------------------------------
后记之二:这个方法是下下策了,只有在前面的方法都无效时才使用。
在action中直接使用request.getParameter()时;还是出现乱码。原因分析如下:
1、getParameter()是有带字符参数的。例:
String s = (String)request.getParameter("txt").getBytes("iso-8859-1");
2、String也可以带有字符参数。
String(byte[] bytes, String charsetName) 构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
例:String s = new String("中文","utf-8");
3、综合上述两点,编写一个类来完成此项任务
public class ConvertCharacter{
public String Convert(String s){
String result;
byte[] temp ;
try{
temp = s.getBytes("iso-8859-1");
result = new String(temp,"utf-8");
}
return result;
}
}
request.getParameter乱码的问题
方法一:
通过设置tomcat的配置文件server.xml
Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" uRIEncoding="gbk"/>
方法二:
1: String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");
后记:
jsp部分 html部分
如:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
如果连接了数据库, 最好把数据也考虑在内,
本文介绍了在数据库操作中遇到的中文乱码问题及其解决方案。通过调整数据库连接参数sendStringParametersAsUnicode为true,确保字符串参数以Unicode格式发送,解决了中文字段在数据库层的乱码问题。
175

被折叠的 条评论
为什么被折叠?



