Dashboard > WebWork 1 > WebWork CookBook > Using CommandDriven Actions
  WebWork 1 Log In View a printable version of the current page.  
  Using CommandDriven Actions
Added by Mike Cannon-Brookes, last edited by Hani Suleiman on Apr 25, 2004  (view change)
Labels: 
(None)

(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.

What are the use cases for CommandDriven?

Posted by Joseph Ottinger at Nov 09, 2002 04:49 | Permalink

If you find yourself in a situation where Actions share the same inputs, but do slightly different things, consider CommandDriven. Obviously, this isn't an incitement to force completely conceptually different Actions together.

Posted by Anonymous at Nov 11, 2002 05:11 | Permalink

Just in case anyone else get's stuck...

ActionSupport uses getClass().getMethod() to get a Method object for each command (which it the invokes). This means that your doXXX() methods MUST be public member methods.

If I'm only stating the obvious to you, the please ignore the above

Posted by Anonymous at Jan 22, 2003 17:31 | Permalink

When using command driven actions, how does someone do the validation for each individual doXXX, if they require different (or no) validation from the others?

Posted by Anonymous at Jun 06, 2003 02:34 | Permalink

I'd like to have multiple submit buttons within a single form, each invoking a different do* method on my CommandDriven action. It seems that if the "value" attribute on the submit button is set to the method name, i.e. doStuff() method and a button value is "stuff", that it works.

However, this restricts me to poorly named buttons (at least the users don't like the names And there is no way to change what shows up on the rendered web page without changing my method names. Is there a better way to accomplish multiple submit buttons on a form without some convoluted onclick() code where I need to build up the url by hand? - Thanks

Posted by Anonymous at Dec 05, 2003 13:01 | Permalink

I think i also need a good sample how to use CommandDriven action with multiple submit button, because it can reduce duplication in form and the action. -Thanks

Posted by Anonymous at Jan 06, 2004 00:26 | Permalink
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators