responseText从action接收中文乱码

jlpf 2009-02-23
我遇到了一个问题:
jsp 发出一个ajax request给 action, action 返回来的字符串含有中文字母,在 javascript中打印就是乱码(obj.responseText)。


struts2.properties中,我添加了
struts.i18n.encoding=UTF-8


jsp也定义了编码
<%@ page contentType="text/html; charset=UTF-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

tomcat的server.xml中,我也添加了
URIEncoding="UTF-8"


是不是在action中还需要自定义下编码呢?怎么设定?谢谢

lijie250 2009-02-24
你把ACTION的代码给我看下!
jlpf 2009-02-24
看来信息不全啊。

struts.xml
<action name="getclientgroup" class="com.sample.ClientJsonAction" method="getClientGroup">
           <result name="success" type="stream"> 
       <param name="contentType">text/xml</param> 
      <param name="inputName">is</param> 
     </result>
        </action>


Action 代码

public class ClientJsonAction extends ActionSupport {


protected String buildClientGroupResponse() {
    ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
    //fetch data
List<ClientgroupVO> clientgroups=userManager.listClientGroup();
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("UTF-8");
Element root = document.addElement("groups");
Iterator itr = clientgroups.iterator();
Element group, id,name,member;
while (itr.hasNext())
{
ClientgroupVO clientgroup=(ClientgroupVO)itr.next();

group = root.addElement("group");
id = group.addElement("id");
id.setText(new Integer(clientgroup.getGroupid()).toString());
name = group.addElement("name");
name.setText(clientgroup.getGroupname());

Set<ClientMachineVO> clients=clientgroup.getClientCollection();
Iterator itrclient = clients.iterator();
while (itrclient.hasNext())
{
ClientMachineVO client=(ClientMachineVO)itrclient.next();
member = group.addElement("member");
id = member.addElement("id");
id.setText(new Integer(client.getId()).toString());

name = member.addElement("type");
name.setText(getTypeShowStr(client.getType()));
name = member.addElement("status");
name.setText("1");
name = member.addElement("name");
name.setText(client.getName());




}
}


System.out.println(XmlUtil.document2String(document));

return XmlUtil.document2String(document);
}
    ByteArrayInputStream is;
    public String getClientGroup() throws Exception
{

ByteArrayOutputStream os=new ByteArrayOutputStream(); 
        String str= buildClientGroupResponse();
        try {
os.write(str.getBytes());      
   
is=new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

       
return SUCCESS;
}

}

lijie250 2009-02-24
str.getBytes()改成str.getBytes("utf8")试试
jlpf 2009-02-24
还不行  
jlpf 2009-02-24
可以了,我弄错了,谢谢lijie250 !!
lijie250 2009-02-25
不设置编码会是本地默认编码!
JOMBO 2009-02-27
Ajax.Request 中有个提交方式 改为post

dengjianli 2009-04-07
在你的action声明 private HttpServletResponse response;
response = ServletActionContext.getResponse();
response.setContentType("application/xml;charset=UTF-8");
response.getWriter().write(”你的东东“);

zqb666kkk 2010-11-21
我也遇到了这种问题,可是我按照楼主的方法 却解决不了!
Global site tag (gtag.js) - Google Analytics