On Sun, 7 Jul 2002, Craig R. McClanahan wrote:

> Date: Sun, 7 Jul 2002 16:51:25 -0700 (PDT)
> From: Craig R. McClanahan <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Re: Doing business logic validation in the Action
>
>
>
> On Sun, 7 Jul 2002, James Turner wrote:
>
> > Date: Sun, 07 Jul 2002 19:40:59 -0400
> > From: James Turner <[EMAIL PROTECTED]>
> > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> > To: [EMAIL PROTECTED]
> > Subject: Doing business logic validation in the Action
> >
> > Consider the following:  You have a new user creation form, and need to
> > ensure that the username chosen doesn't already exist in the database.
> >
> > To keep any business logic out of the ActionForm, you'd really like to do
> > this in the Action.  However, as near as I can tell, the Action doesn't
> > have any access to the ActionErrors returned from the form, which means
> > that you'd have to use another mechanism (like a request attribute) to
> > return these type of errors.
> >
> > Am I missing something, or is this the best practice for business logic
> > validation of forms?
> >
>
> The struts example app does a "validation check" like this in the Action
> instead of in the validate() method, for exactly the reason you describe.
> See the source for LogonAction to see how you can create and save error
> messages directly in an Action, and return to the right input page as
> defined for the current mapping.
>

Expanding on this slightly -- the most recent nightly builds have
supported new features to treat the "input" attribute of an <action>
element as the name of a <forward> instead of a context-relative path.  To
avoid having to deal with that kind of complexity inside your action,
there is a new method on ActionMapping called getInputForward() that
returns an ActionForward back to the input page, no matter how the
configuration stuff works.  So, you'll see LogonAction in the most recent
nightly builds do this:

  ActionErrors errors = new ActionErrors();
  ... test for error conditions and add ActionError instances ...
  if (!errors.empty()) {
    saveErrors(request, errors);
    return (mapping.getInputForward());
  }

You'll need nightly build 20020707 or later for this to work.

> > James
>
> Craig
>

Craig


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

Reply via email to