struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jae K" <>
Subject [S2] action mapping best practice
Date Mon, 02 Apr 2007 22:39:01 GMT
I have a comment about the MailReader tutorial.

This is the action declaration for the Login action:

<action name="*Login_**" method="{1}" class="mailreader2.Login">...
<action name="*Login*"** class="mailreader2.Login">...

To invoke this action, the user agent would first GET request Login_input,
and then POST to Login to submit the form or Login_cancel to cancel, etc.

IMHO this is a *bad* way to declare your actions. Here are the reasons:
1. There is a duplication of URLs. There are at least two URL resources with
the login form: /Login_input and /Login (/Login will display the form if
there was a field error)
2. There is a duplication of action declarations. In this example there are
two declarations, one for Login_* and Login.
3. There are needlessly many methods in the action class.
4. It's not a restful interface

The source of the problem is with the ValidationInterceptor class (and its
subclasses). Whether or not validation occurs should not depend solely on
the method name (input, submit, etc), but also the HTTP verb (GET/POST). A
more intelligent ValidationInterceptor subclass would solve all of these

1. I created a PostMethodValidationInterceptor that extends
ValidationInterceptor to validate only on POST requests.
2. My action support superclass has two methods: isInvalid() and

Now I only need one action mapping, and only one method in my action class
as follows:

public class Input extends MySupport {
   public String execute() throws Exception{
     if(!isValid() || !isFormPosted)
       return "input";
     //continue with input logic

The isValid and isFormPosted methods are very easy to implement. Let me know
if anybody wants any of my source files.

 - Jae

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message