...
Both Struts 1 and Struts 2 can return any type of response. We are not limited to forwarding to a server page. In Struts 1, you can just do something like:
Code Block |
---|
title | Struts 1 Action fragment |
---|
|
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World! This is an AJAX response from a Struts Action.");
out.flush();
return null;
}
|
In Struts 2, we can do the same thing with a Stream result.
Code Block |
---|
lang | java |
---|
title | Struts 2 Stream result 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;
}
}
|
Code Block |
---|
lang | XML |
---|
title | Struts 2 Configuring the TextResult Action |
---|
|
<action name="text-result" class="actions.TextResult">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
|
...