日期:2014-05-18  浏览次数:20617 次

struts2 输出xml乱码问题,急!!!求大神帮解决
服务器代码如下

  private void outPutXml2(String xml) throws IOExceptionSAXException, ParserConfigurationException{
  HttpServletResponse response = ServletActionContext.getResponse();      
      PrintWriter out;
      out = response.getWriter();
      response.setCharacterEncoding("UTF-8");  
      response.setContentType("text/xml;charset=UTF-8");   
      response.setHeader("Cache-Control", "no-cache");
      out.write(xml);
  }
  public void getCities() throws Exception {
    List citys = this.userService.getCities(this.user);
    
    
   Document doc = DocumentHelper.createDocument();
   Element rootElmt = DocumentHelper.createElement("result");
   doc.add(rootElmt);
   Element psElmt = DocumentHelper.createElement("cities");
   rootElmt.add(psElmt);
   psElmt.addAttribute("size", String.valueOf(citys.size()));
   for(Object city : citys){
   psElmt.add(new DOMElement("city").addText(String.valueOf(city)));
   }
  
   this.outPutXml2(this.prettyXml(doc).getBuffer().toString());
  }

这里面用
response.setContentType("text/xml;charset=UTF-8"); 

设置了响应的编码,但是到浏览器,却被改变了

Cache-Control:no-cache
Content-Length:111
Content-Type:text/xml;charset=ISO-8859-1
Date:Sat, 30 Nov 2013 02:09:37 GMT
Server:Apache-Coyote/1.1

struts2 配置
 

<action name="getCities" class="com.bang.action.XMLAction" method="getCities">
<result name="xmlMessage" type="plainText">
<param name="charSet">UTF-8</param>
</result>
</action>

再说明一下,其他直接返回jsp页面的编码是utf-8,只有我这个直接输出xml的编码被改成ISO-8859-1,不知道是tomcat问题还是struts问题。tomcat里面的端口编码配置我已经配置成utf-8,一样没有效果。网上查了,都没有类似的问题,希望大神们热心帮我解决下,感激不尽