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就是字符集的设定了。
<!-- 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中设定字符编符.
--------------------------------------------------------------------
后记之一:在使用struts2时,仍是遇到一种乱码。后来调试才发现,struts2的web.xml配置是有顺序的。
按照Struts2的API,filter的顺序是 struts-cleanup filter SiteMesh filter FilterDispatcher
--------------------------------------------------------------------
在action中直接使用request.getParameter()时;还是出现乱码。原因分析如下:
String s = (String)request.getParameter("txt").getBytes("iso-8859-1");
String(byte[] bytes, String charsetName) 构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
例:String s = new String("中文","utf-8");
request.getParameter乱码的问题
方法一:
通过设置tomcat的配置文件server.xml
Connector port="8080" maxHttpHeaderSize="8192"
方法二:
1: String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");
原文链接:http://topic.csdn.net/u/20090113/10/894b7d73-34b5-4664-8bc6-cd641074b12b.html