(by Simon Stewart)

Assume that you have an Action that takes the same inputs, but performs different actions depending on how it's called. By implementing CommandDriven (which is a single method that ActionSupport already implements for you) it is possible to do this. To illustrate, consider this action:

public class FooAction extends ActionSupport implements CommandDriven
{
     String name;

public FooAction() { name = "world"; } public String getName() { return name; } public void setName( String name ) { this.name = name; }

public String doExecute() { return Action.SUCCESS; } public String doGoodbye() { return Action.SUCCESS; } }

You'll notice that it doesn't implement an execute method directly because ActionSupport does this already: if you want CommandDriven Actions to work as expected, you must leave the execute method alone! What it does have is a "doExecute" and a "doGoodbye" method.

The accompanying views.properties for this is:

greet.action=FooAction
greet.success=hello.vm
greet.goodbye.action=FooAction!goodbye
greet.goodbye.success=goodbye.vm

The only thing that's unusual about this view is the action definition for "greet.goodbye" --- it's got "!goodbye" appended to the Action's class name. This declares that instead of performing the default action, which is doExecute(), when this alias is hit the doGoodbye method should be used instead.

Note that the do* method should be public, return a String and have no parameters or it won't be found.

Why is this useful? Well, it's simply an alternative way of going about certain things. Let's say for example you have a set of pages that perform CRUD (create, read, update, delete) operations on some object. One possible way to implement this would be to have one base action class that deals with looking up the object and other such 'common' functionality, with subclasses for each of the CRUD operations. Alternatively, you could use commands, with one action class defining doUpdate(), doDelete(), doCreate(), with the doExecute perhaps handling the read view.

Finally, it is important to note that validation is NOT called automatically for command methods. If you need validation then you should call doValidate() explicitly within your commands.