有些时候我们只想做个简单的ajax测试,但是又不想写serverlet就用struts2,这时候就需要返回一个简单的response就可以了。
package actions;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;
public class TextResultAction extends ActionSupport {
privateInputStream inputStream;
publicInputStream getInputStream() {
return inputStream;
}
publicvoid setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
publicString execute() throws Exception {
inputStream = new StringBufferInputStream(
"Hello World! This is a text string response from a Struts 2Action.");
return SUCCESS;
}
}
struts中的配置
<action name="text-result" class="actions.TextResultAction">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>