...
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 |
---|
|
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;
|
...
Note |
---|
title | There are easier ways! |
---|
|
Using a Struts 2 plugin (e.g., JSON plugin, jQuery plugin, etc.) is, in general, preferred to writing the response directly from within an action. See sections following this for further details. |
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 ByteArrayInputStream("Hello World! This is a text string response from a Struts 2 Action.".getBytes("UTF-8"));
return SUCCESS;
}
}
|
Code Block |
---|
lang | XMLxml |
---|
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>
|
...
While Struts works fine with Ajax out-of-the-box, for added value, several Ajax-centric plugins are available.
Ajax Tag Plugins
- jQuery - The jQuery Plugin provide ajax functionality and UI Widgets an JavaScript Grid based on the jQuery javascript framework.
- Ajax Parts - The AjaxParts Taglib (APT) is a component of the Java Web Parts (JWP) project (http://javawebparts.sourceforge.net
Image Removed) that allows for 100% declarative (read: no Javascript coding required!) AJAX functionality within a Java-based webapp. - Dojo - The Ajax Tags Dojo Plugin was represented as a theme for Struts 2.0. For Struts 2.1, the Dojo tags are bundled as a plugin until version 2.3.x. Since version 2.5 this plugin is not part of th Struts2 distribution anymore
- YUI - The Yahoo User Interface (YUI) Plugin has only a few tags are available so far, but the YUI tags tend to be easier to use than the Dojo versions.
- jQuery - The jQuery Plugin provide ajax functionality and UI Widgets an JavaScript Grid based on the jQuery javascript framework..
Other Ajax Plugins
- Ajax File Upload - With the Ajax File Upload Plugin we can upload a file to the server and asynchronously monitor its progress.
- GWT - The Google Web Toolkit Plugin exposes Struts 2 actions to the GWT RPC mechanism.
- JSON - The JSON Plugin serializes Actions properties into JSON, making it easy to respond to JavaScript requests.
...
While server pages are most often used to generate HTML, we can use server pages to create other types of data streams. Here's an example:
Code Block |
---|
|
<%@ page import="java.util.Iterator,
java.util.List,
com.esolaria.dojoex.Book,
com.esolaria.dojoex.BookManager" %>
<%
String bookIdStr = request.getParameter("bookId");
int bookId = (bookIdStr == null || "".equals(bookIdStr.trim()))
? 0 : Integer.parseInt(bookIdStr);
Book book = BookManager.getBook(bookId);
if (book != null) {
out.println(book.toJSONString());
System.out.println("itis: " + book.toJSONString());
}
%>
|
...