Struts Flow

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.

Features

  • Easily script complex workflows
  • Full access to Struts features
  • Can exist side-by-side regular Struts actions
  • Ability to run in non-Struts environments (uses Jakarta's Commons-Chain)
  • Enhanced Java API methods and Collections integration
  • Remote RPC support (termed Ajax but with JSON instead of XML) for calling flow methods from the client
  • Includes Wizard library to help easily create complex wizards
  • Includes Javascript Templates library to replace JSP for a 100% Javascript view layer.
  • Includes number guessing, remote rpc, Javascript Templates, and wizard examples

Documentation and Examples

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.

What's New

0.5 - Unreleased

  • Added better Collections support within Javascript
  • Upgraded Rhino library to 1.6 which features native continuations support
  • Added framework for adding methods and properties to core Java API classes
  • Added JSON support
  • Added client Javascript library and server-side support for calling flow functions from the brower
  • Added Javascript Templates library for generating HTML content on the server and client side
  • Moved project to Apache Struts
  • Added new remote number guessing game example to demonstrate remote Javascript flow calls
  • Added new templates number guessing game example to demonstrate a 100% Javascript application
  • Added new documentation generated from the Java and Javascript source files

0.2 - September 10, 2004

  • Added wizard library to make wizard creation easy
  • Added wizard example
  • Updated and improved documentation
  • Fixed a few bugs

0.1 - June 3, 2004

  • Hooks into Struts' logging
  • Added continuation expiration system
  • Added support for JavaScript debugger
  • Improved Struts integration
  • Added full JavaDocs
  • Added many more configuration options

Initial Import - May 12, 2004

  • Example working
  • Struts integration functioning
  • Needs better Struts form integration
  • Needs to hook into Struts' logging

Requirements

Struts-specific features of Struts Flow requires Struts 1.1 or greater.