struts2中文乱码解决方法
来源:互联网 发布:linux 后台运行 nohup 编辑:程序博客网 时间:2024/05/18 21:40
终于在自己的项目中引入struts2了,但一上来就来一个中文乱码的问题。google了半天找了几个不痛不痒的结果,很是不满意。又调试了半天,终于解决了中文乱码的问题。总结一下,中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。大致的原理是java使用unicode编码-->window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种编码方法),这样转来转去就乱码了^_^。解决方法如下:
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
跟上述方法,类似还有在action中设定字符编符.
HttpServletResponse response = null; response =ServletActionContext.getResponse(); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
通过上述方法,基本就可以搞定中文乱码的问题了。当然,也有例外(如webserver的版本/数据库的版本等等)。象在我的一个项目碰到一个中文乱码,tomcate5.5是会乱码的,而在tomcate6中就不会。这边就涉及到tomcate connector字符的设置了。
--------------------------------------------------------------------
后记之一:在使用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");
- struts2中文乱码解决方法
- Struts2中文乱码解决方法
- Struts2中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- Struts2中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- STRUTS2 中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法 .
- struts2中文乱码解决方法
- struts2 中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- struts2中文乱码解决方法
- NET程序安装包制作
- rails页面跳转(render和redirect_to)
- window xp IIS 安装遇到的错误并安装不成功
- 对计算机科学中的哲学价值的一点思考
- 男人不可犯的禁忌
- struts2中文乱码解决方法
- 什么是ESD?
- 区分JS中的undefined,null,"",0和false
- 平行机资料
- ASP导出Excel数据最簡單的方法
- 转:关于VC中的"stdafx.h"
- MathType 6.0 中某些符号不能正确显示 (乱码) , MT Extra (TrueType) 字体问题解决
- 第一次在csdn写日记--记第一个自己的项目
- 网站内部优化技巧