2013年12月1日星期日

struts2 xml garbled output , ME ! ! ! Find Great God help solve

Server code as follows

  private void outPutXml2(String xml) throws IOException, SAXException, 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());
  }
There used
response.setContentType("text/xml;charset=UTF-8"); 

response encoding settings , but to the browser, but was changed

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 configuration

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

explain once again , other direct return jsp page encoding is utf-8, only my direct output xml code was changed to ISO-8859-1, I do not know struts tomcat problem or question . tomcat port encoding scheme which I've configured utf-8, as there is no effect. Online search , no similar problems , I hope the great God who helped me solve enthusiastic , grateful
------ Solution ------------------ --------------------------
plus an encoding filter try
------ Solution ---- ----------------------------------------
do not know what your specific needs are ? ? ? Specific to do ah ? ? ?
------ Solution ---------------------------------------- ----

specific needs is to do an interface sends a request over the other , I used xml output data to each other   ajax request sent with it ? ? ?
------ Solution ---------------------------------------- ----
Is not it manual stitching xml string.
------ For reference only -------------------------------------- -


<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

has spent
------ For reference only ------------------------------- --------

specific needs is to do an interface sends a request over the other , I use xml to each output data
------ For reference only ---- -----------------------------------
plus struts in your strut.xml file. i18n.encoding = UTF-8 sentence . . .
------ For reference only -------------------------------------- -

this is a coded request parameters , instead of the corresponding output encoding it
------ For reference only ----------------- ----------------------

specific needs is to do an interface sends a request over the other , I used xml output data to each other        ajax request sent with it ? ? ?   What sends the request is not the key , the key is the corresponding garbled , not a request garbled
------ For reference only ----------------- ----------------------
# type of change in the value of CODE_PRE_6 # httpheader, you try it , this is my guess . Or not , then it had to wait for a master. . .
------ For reference only -------------------------------------- -
stitching also be output ah
------ For reference only ---------------------------- -----------
resolved, Lv , for another way
direct extension struts2 result set StrutsResultSupport:
http://blog.csdn.net/d8111/article/details/2914079

没有评论:

发表评论