[size=medium]有时候ajax应用只需要服务器返回一个字符串而不是一个jsp页面。可以用如下方法实现。将action的返回类型设为stream,将字符串写入字符串输入流,返回给客户端。示例代码如下
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;
public class TextResult extends ActionSupport {
private InputStream inputStream;
//省略get方法
public String execute(){
inputStream = new StringBufferInputStream("This is a string response");
return SUCCESS;
}
}
<action name="text-result" class="actions.TextResult">
<result type="stream">
<param name="inputName">inputStream</param>
</result>
</action>
据jdk1.6上说, StringBufferInputStream类已经过时。建议使用StringReader。但是经我查看源代码,stream返回类型只支持InputStream的子类。[/size]
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;
public class TextResult extends ActionSupport {
private InputStream inputStream;
//省略get方法
public String execute(){
inputStream = new StringBufferInputStream("This is a string response");
return SUCCESS;
}
}
<action name="text-result" class="actions.TextResult">
<result type="stream">
<param name="inputName">inputStream</param>
</result>
</action>
据jdk1.6上说, StringBufferInputStream类已经过时。建议使用StringReader。但是经我查看源代码,stream返回类型只支持InputStream的子类。[/size]