Struts Flow is a port of Cocoon's Control Flow to Struts to allow complex workflow, like multi-form wizards, to be easily implemented using continuations-capable JavaScript. It provides the ability to describe the order of Web pages that have to be sent to the client, at any given point in time in an application. The flow scripts fulfill the role of Struts Actions, however, they can be used along side traditional Java-based Struts Actions.
This is an example of a Struts Flow server-side script which logs the user on to an application:
function login() {
userManager = struts.applicationScope["userManager"];
error = "";
while (struts.sessionScope["curUser"] == null) {
forwardAndWait("loginForm", {"error" : error});
user = struts.param["user"];
passwd = struts.param["passwd"];
if (userManager.login(user, passwd)) {
struts.sessionScope["curUser"] = user;
} else {
error = "Invalid login, please try again";
}
}
}
The forwardAndWait() method sends an HTML page to the user and waits for a response. When the form
is submitted, Struts Flow restores the variable values and restarts the script where it left off. There are more
detailed examples in the Examples section.
Since continuations are best implemented in Javascript, Struts Flow also focuses on making Javascript
easier to use and better integrated into the Java environment borrowing several ideas from Groovy. First, information in Collections classes
can be accessed using Javascript array (foo[1]) and object (foo["bar"]) notations.
Also, Struts Flow uses JSON to allow client-side Javascript running
in a browser to call server-side Javascript flow functions. Javascript Templates
can be used on the server and client side with Struts Flow to support a 100% Javascript view layer.
Finally, core Java API classes like java.io.File have additonal methods and properties
added to them. For example, to process each line of a text file, you can use the following closure:
file = new java.io.File("foo.txt");
file.eachLine(
function(line) {
print(line);
}
);
While the initial target of the extracted Control Flow is Struts, the Flow code and Javascript extensions are reusable from other non-Struts environments. This means Control Flow could be used to drive non-Struts JSP applications, portlets, or even complex web services.
The quickest way to understand how Struts Flow works is to take a look at the examples below, and even to download and run them yourself. Once you have your "hello world" application running, use the "Built-in Resources" section on the left to learn what methods and variables Struts Flow makes available to every script. Finally, Struts Flow enhances the standard Java API by adding methods and properties to many classes to keep your Struts Flow scripts short and to the point. A list of the extensions is also on the left under the heading"Java Enhancements".
The following examples show how Struts Flow can be used:
For more documentation about the general concept of flow and continuations, the Apache Cocoon Control Flow pages might be useful as the core of Struts Flow was originally extracted from Cocoon.