1
回答
struts2.2.1 和jQuery 进行ajax组合出现中文字符问题。
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我按照struts2.2.1说明文档试试和jQuery的组合,按照如下的设置返回英文没问题。如把返回字符换成 中文字符,就会出现乱码。这里该怎么设置呢。不要说通过response 得到out 输出哦。。。。。。

struts2 action

package actions;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;

public class TextResult extends ActionSupport  {
    private InputStream inputStream;
    public InputStream getInputStream() {
        return inputStream;
    }

    public String execute() throws Exception {
        inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
        return SUCCESS;
    }
}

struts.xml

<struts>

 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
  <default-action-ref name="index" />
  <action name="ajaxtest" class="com.ray.action.AjaxAction">
   <result name="success" type="stream">
    <param name="contentType">text/html</param>
    <param name="inputName">inputStream</param>    
   </result>
  </action>
 </package>
 <!-- Add packages here -->
</struts>

对于result的参数如下:

stream Result

     

     

  • contentType - the stream mime-type as sent to the web browser (default = text/plain).

     

     

  • contentLength - the stream length in bytes (the browser displays a progress bar).

     

     

  • contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".

     

     

  • inputName - the name of the InputStream property from the chained action (default = inputStream).

     

     

  • bufferSize - the size of the buffer to copy from input to output (default = 1024).

     

     

  • allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)

     

     

  • contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

 

举报
禹帆
发帖于7年前 1回/1K+阅
顶部