On 2/7/06, Michael Jouravlev <[EMAIL PROTECTED]> wrote:
> On 2/7/06, Tom Ansley <[EMAIL PROTECTED]> wrote:
> > I guess the big thing I do not understand here is why validator is being
> > called when the form has not even had time to initialize.
> >
> > In my application I rely on the action.execute() method being the first to
> > be called because it allows me to prepopulate the form associated with the
> > action.  This makes good sense.  Then, the jsp page is created.  Then, when
> > the reply comes back from the user, that is when the validation should
> > occur.
> >
> > Is this assumption about the action.execute() method being the first thing
> > called a BIG mistake?  What happens if the form needs to retrieve a number
> > of lists which are to be displayed in the JSP page?  I do this retrieval in
> > the action.execute and place the lists in the form.  Should I be doing it
> > somewhere else?
> >
> > Thanks so much to all for their previous thoughts by the way.
>
> Do not use automatic validation, it screws up action lifecycle.
> Anyway, the call sequence is:
>
> * ActionForm.reset
> * populate ActionForm from request
> * ActionForm.validate if "validate"==true
>   ** if ActionForm.validate returns non-null object, Struts skips
>      action class altogether, and selects path corresponding to
> "input" property.
>   ** If validate() returns null, then execute() is called on action class.
>
> See? Turn autovalidation off and throw out "input" attribute,
> everything will be much more straightforward. Also, if you have not
> got it yet, "input" does not mean "this is input page where I start
> off filling out my form", it means "this is the page to show in case
> of error".
>
> About preparing form for display: if you can ensure that your input is
> sent via HTML FORM, then you can choose POST method only for form
> submission, and then differentiate by request type: POST means
> postback, GET means initial render.

I just wanted to clarify this: "Then, when the reply comes back from
the user, that is when the validation should occur." Struts is a tiny
wrapper over servlets, which are a tiny wrapper over HTTP, and HTTP
does not know when "reply comes back from the user". All HTTP knows
about is that request was received. It is your job to decide what type
of request is it: is it a request for initial page render (usually GET
with no parameters), or is it a so-called postback (usually POST with
a bunch of parameters). Most likely, on initial page render you would
prefer to set up the page and *do not* validate input. Contrary, on
postback, you would *want to validate* input data. Therefore, if you
use the same action mapping for render and for postback, you need to
differentiate somehow between these two modes.

Another choice is to use dispatch-type action. In that case, when your
link or button is activated on a form, an appropriate method will be
called. You just process an input event without caring much what
exactly happened (it was a postback).

If you don't want to use dispatch-type action, and you do not want to
choose render or validate based on request method, then you are left
with classic (and sucky) Struts pattern of using pre- and post-
actions. See here for more insight:
http://www.jroller.com/page/javadujour?entry=code_behind_for_struts I
personally don't like "pre- and post- action" pattern, I believe that
it is the primary reason for Struts app to be convoluted, but maybe
you will have a different opinion.

With your current setup I recomment dispatch-type action and switching
logic based on HTTP method.

Michael J.

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

Reply via email to