struts2中文乱码解决方法
2009-11-02 15:31
447 查看
关于乱码的解决方法,网上成车成车的。整体要领无非就是统一编码格式。
总结struts2项目中遇到乱码的解决方法。
struts2中文乱码解决方法
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
struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
跟上述方法,类似还有在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字符的设置了。
--------------------------------------------------------------------
在使用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中文乱码解决方法
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
struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
跟上述方法,类似还有在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字符的设置了。
--------------------------------------------------------------------
在使用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项目插入中文到mysql数据库乱码的解决方法
- struts2 GET方法提交乱码,地址栏输入中文地址乱码解决
- struts2中文乱码解决方法
- struts2中文乱码解决方法 (转载)
- struts2中文乱码解决方法
- struts2中文乱码解决方法(post请求)
- Struts2 get方法传递 中文 到Action乱码解决
- struts2 中文乱码问题,自定义过滤器通用解决方法
- Struts2 get方法传递 中文 到Action乱码解决
- 关于Struts2使用redirectAction方式在两个Action之间传值时中文乱码问题的解决方法
- struts2中文乱码解决方法
- 使用struts2的<s:param>等标签向action传值出现中文乱码的解决方法
- struts2 url 中文乱码问题解决方法
- struts2中文乱码解决方法
- Struts2中文乱码解决方法
- Struts2重定向时,OGNL异常(中文乱码)解决方法。
- struts2中文乱码问题的解决方法
- struts2中文乱码问题的解决方法
- struts2中注解redirect时传递参数中文乱码问题解决方法