终于在自己的项目中引入struts2了,但一上来就来一个中文乱码的问题。google了半天找了几个不痛不痒的结果,很是不满意。又调试了半天,终于
解决了中文乱码的问题。总结一下,中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。大致的原理是java使用unicode编码--
>window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种编码方法),这样转来转去就乱码了
^_^。解决方法如下:
1. 在struts2里面,最好将所有字符都设成utf-8。
<%@ page contentType="text/html;
charset=UTF-8"%> <%@ page
pageEncoding="UTF-8" %>
|
1.1 在jsp页面设定字符编码。这边有必有说明的是如果是jsp+java
bean+servlet的方案,中文乱码很好解决,统一设成gb2312就可以了。
1.2 使用struts框架字符集不能设成gb2312,要改成utf-8。
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,
StringcharsetName)
构造一个新的
String,方法是使用指定的字符集解码指定的字节数组。
例:String s = new String("中文","utf-8");
3、综合上述两点,编写一个类来完成此项任务
public class ConvertCharacter{
public String
Convert(String s){
Stringresult;
byte[] temp ;
try{
temp = s.getBytes("iso-8859-1");
result = new String(temp,"utf-8");
}
return result;
}
}
|
相关推荐
struts中文乱码问题解决详细步骤(两种方法)
在struts2中解决页面中出现的乱码问题,写在web.xml中
struts2中文乱码的解决DEMO,绝对可以运行。
Struts中中文乱码的解决方法 Struts中中文乱码的解决方法 Struts中中文乱码的解决方法
struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量<constant name="struts.i18n.encoding" value="UTF-8"/>...关于Struts2各个版本、各种过滤器,用get方式提交都方法都无法解决的中文乱码问题。
Struts2乱码解决办法,个人测试有效
Struts中文乱码解决方案
解决Struts2.0中文乱码详细方法。
解决中文乱码的问题
struts 中文乱码问题解决
struts2中存在中文乱码的问题解决方 中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。 大致的原理是java使用unicode编码-->window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种编码...
struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充
struts1.2 解决中文乱码(Java技术)
struts2中文乱码
struts中中文乱码问题解决方法,并对其进行了总结。
struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结
在进行web应用程序开发的过程中,中文问题经常困扰着很多程序员。因此,在接下来的两个课程的学习中,我将根据实践经验来谈一下web应用中的中文问题的解决方法。让大家不再为此而头痛。
Struts2资源文件在jsp页面中显示乱码解决
struts,ajax乱码解决方案