Hello all,

On this page:
http://wiki.apache.org/struts/StrutsActionRelease200

There is, under the new features heading a reference to the
validation/workflow issues brought up on this page:
http://wiki.apache.org/struts/RoughSpots?action=show

Under the main list, item number 16, and under Patrick's list, item
number 4 and 5, there is discussion of the default workflow and its
relation to the idea of view vs. update requests to the same action.

I haven't seen any discussion of this on the dev list, and i'm wondering
if anyone is actively working on this and what directions are being
considered. In trying to work this out for myself, i came up with a
simple solution for the main use case, and i would like to share it here
and ask for comments. 

The main idea is that the Prepareable interceptor is used to setup the
view, and another interceptor called SubmittableInterceptor is added to
the end of the default interceptor stack and is used to determine if a
form submission has occured. A marker interface called Submittable
allows the user, via a boolean wasSubmitted method, to determine how the
action will determine if a form submission has occured, whether it be
the POST vs GET idea, the inclusion of a hidden flag field, or the
presence of a submit or button parameter. It also has a "submit" method
that can be used optionally to process the submission and return a
result string. 

The SubmittableInterceptor will see if wasSubmitted returns true, and if
so, run the submit method. If the submit method returns a result string,
the interceptor returns that, otherwise the interceptor stack invocation
continues. If the form was not submitted, the interceptor returns the
INPUT result, displaying the form, which has been setup by Prepareable.

My BaseAction extends ActionSupport and implements Submittable and
allows for the inclusion of the SubmittableInterceptor into the default
stack, with default behavior that does nothing.

The Validate method of the action will also check wasSubmitted to
determine if it should validate, this could be eliminated by either
putting SubmittableInterceptor before the DefaultWorkflowInterceptor or
by putting the SubmittableInterceptor logic in
DefaultWorkflowInterceptor.

The code:

public interface Submittable {

    boolean wasSubmitted();
    String submit();
}

SubmittableInterceptor intercept method:
public String intercept(ActionInvocation invocation) throws Exception {
   Object action = invocation.getAction();

   if (action instanceof Submittable) {
      Submittable submittable = (Submittable) action;
      if (submittable.wasSubmitted()) {
         String submitResult = submittable.submit();
         if (submitReturn != null && ! submitReturn.equals("")) {
            return submitResult;
         }
      }
      else {
         return Action.INPUT;
      }
   }

   return invocation.invoke();
}

public class BaseAction extends ActionSupport implements Submittable  {

    public boolean wasSubmitted() {
        return true;
    }

    public String submit() {
        return null;
    }
}

public class TesterAction extends BaseAction implements Preparable,
ServletRequestAware  {

    private HttpServletRequest request;

    public void setServletRequest(HttpServletRequest r) {
       this.request = r;
    }

    private String act = "";
    private String name = "";

    public String getAct() {
       return this.act;
    }
    public void setAct(String s) {
       this.act = s;
    }

    public String getName() {
       return this.name;
    }
    public void setName(String s) {
       this.name = s;
    }

    public void validate() {
       if (wasSubmitted()) {
          if (name.equals("")) {
             addFieldError("name", "required");
          }
       }
    }

    public void prepare() {
       // setup view
       request.setAttribute("form_name", "Test Form");
    }

    public boolean wasSubmitted() {
       if (act.equals("")) {
          return false;
       }
       else {
          return true;
       }
    }

    public String submit() {
       // allow execute to do business logic
       return null;
    }

    public String execute() throws Exception {
       // do business logic
       return SUCCESS;
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to