struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin Cooper" <martin.coo...@tumbleweed.com>
Subject Re: Automatic Form Validation - A further question
Date Fri, 03 Aug 2001 04:16:47 GMT
The only time your form bean's validate() method will be called by Struts is
when Struts has populated the form bean from a request and is about to call
your action's perform() method. So if validate() is being called, then a
request is being processed for an action mapping which specifies an
associated form bean.

There are two situations in which Struts will instantiate your form bean for
you:

1) When a request is received for an action mapping which names a form bean,
and that form bean does not already exist. In this case, as mentioned above,
Struts will populate the bean from the request and then call validate().

2) When processing an <html:form> tag which references a form bean, and that
form bean does not already exist. In this case, Struts will not populate the
bean, and will not call validate().

I understand that you want to go through the action before going to the JSP.
This is definitely the right thing to do. Let me try to explain my previous
suggestion in a little more detail.

Use two action mappings, both referring to the same Action class. One of
them will be exactly the same as what you are using now. The other will look
similar, but will not specify the 'form' attribute.

For the link (or button) which brings you to your action for the first time,
use the second mapping (i.e. the new one). Your action's perform() method
will be passed null for the 'form' parameter, since no form bean was
associated with the mapping. This allows you to easily detect the "first
time" condition. Then, in the action, you create the form bean instance
yourself (since Struts did not do it form you in this case), populate it,
and forward to the JSP.

For subsequent returns to the same action, specify the original action
mapping. In this case, the 'form' parameter to perform() will be non-null,
and you know that the form was created by Struts and populated with the
request parameters.

Hope this helps.

--
Martin Cooper


----- Original Message -----
From: "Emaho, Ghoot" <Ghoot@PETROTECHNICS.co.uk>
To: <struts-user@jakarta.apache.org>
Sent: Thursday, August 02, 2001 1:49 AM
Subject: RE: Automatic Form Validation - A further question


> Backing up in this thread a bit, I'm not clear on what your
> "first time"
> scenario is. If the form validation is being invoked, then a
> request must
> have been submitted with data to populate the form. What is
> it about this
> first request that makes you want to ignore the data for the request?

Not neccessarily true.

It is true if the user requests the JSP, but as I said, I want all
requests to go to the Action first - ie preventing direct JSP access
[which i believe in most cases is poor design]

So therefore, the first time round although the bean is instantiated,
there are no input values as the page with the form has not even been
displayed yet. We are processing the Action on the way to displaying the
form the first time...

Hope this helps

Cheers

Ghoot

>
> --
> Martin Cooper
>
>
> ----- Original Message -----
> From: "Emaho, Ghoot" <Ghoot@PETROTECHNICS.co.uk>
> To: <struts-user@jakarta.apache.org>
> Sent: Wednesday, August 01, 2001 8:26 AM
> Subject: RE: Automatic Form Validation - A further question
>
>
> I have a solution which is acceptable for now.
>
> I have switched automatic validation off for the form bean, although i
> have kept the type/formatting validation logic in the
> validate method of
> the form bean.
>
> I have also added an additional function to the form bean 'isVirgin()'
> which my action class calls. This tells the action wether or not it's
> the first time round. This is achieved by checking all the form bean
> attributes against null - which they are the first time it is created
> [providing you dont initialise the private member variables
> in your form
> bean] As this method is in the form bean itself, the action is unaware
> of how this is determined, allowing it to change in the future.
>
> If the form is a virgin [excuse the language :)] the action justs
> forwards on to the input page. If it's not a virgin, it then calls the
> validate method on the form bean, and then does whatever else it needs
> to do.
>
> This seems to work well and is portable across all my form beans which
> means i have a consistent implementation.
>
> Thanks for your input David
>
> Cheers
>
> Ghoot
>
> > -----Original Message-----
> > From: David Winterfeldt [mailto:dwinterfeldt@yahoo.com]
> > Sent: 01 August 2001 16:11
> > To: struts-user@jakarta.apache.org
> > Subject: RE: Automatic Form Validation - A further question
> >
> >
> >
> > --- "Emaho, Ghoot" <Ghoot@PETROTECHNICS.co.uk> wrote:
> > > Thanks again David.
> > >
> > > I have explored extending ActionForm to check for
> > > and ignore first time
> > > validation. However, it seems from my debug, that
> > > the form bean is
> > > created a new each time. Can someone else confirm
> > > this ?
> > If you have the scope of the action set to 'request',
> > then the bean is only around for the request.  So it
> > would be created each time.
> >
> > >
> > > My further question is this:
> > >
> > > If I choose NOT to use automatic validation, and my
> > > request goes to the
> > > Action first [not the JSP], then what isthe best way
> > > to determine in the
> > > action that this is the 'first time' for this form
> > > bean i.e. it has been
> > > created only as a result of the ActionServlet, not
> > > because it's coming
> > > from the form on a JSP ?
> > >
> > > I know you can check the values of the form bean,
> > > but this seems a
> > > little messy. Any suggestions ?
> > I think checking for values is the best way.  How else
> > can you tell one instance from another?  The only
> > other way I can think of is to have the a class and a
> > subclass(es) that doesn't do anything, but represents
> > what state the bean is in.
> >
> > >
> > > Any help is appreciated, as this is an important
> > > issue for us.
> > >
> > > Cheers
> > >
> > > Ghoot
> > >
> > >
> > > > -----Original Message-----
> > > > From: David Winterfeldt
> > > [mailto:dwinterfeldt@yahoo.com]
> > > > Sent: 31 July 2001 18:43
> > > > To: struts-user@jakarta.apache.org
> > > > Subject: RE: Automatic Form Validation
> > > >
> > > >
> > > >
> > > > --- "Emaho, Ghoot" <Ghoot@PETROTECHNICS.co.uk>
> > > wrote:
> > > > > Thanks for the reply David.
> > > > >
> > > > > Yeah, 'action' checking is another way of
> > > > > determining wether or not to
> > > > > do the validation.
> > > > >
> > > > > I guess I was wondering if it was possible to
> > > > > configure the Form to
> > > > > prevent first time checking ? Would anyone else
> > > find
> > > > > this useful ?
> > > > >
> > > > > Given the recent discussions about preventing
> > > direct
> > > > > acces to JSP's, it
> > > > > seems that the Form validation is designed [in
> > > > > default operation] in
> > > > > such a way as to expect that you have gone
> > > directly
> > > > > to the JSP first.
> > > > > This is one thing I wouldnt really ever want to
> > > do,
> > > > > as I prefer all
> > > > > requests to go through an action - even if it is
> > > > > just to redirect to the
> > > > > JSP [call me a control freak! but there area lot
> > > of
> > > > > others that feel the
> > > > > same :) ]
> > > > I think it is good to go through the controller if
> > > you
> > > > really want to keep the view separate.
> > > >
> > > > >
> > > > > I wonder if the Auto Validation should
> > > accomodate
> > > > > the more common
> > > > > scenario [certainly in larger applications]
> > > where
> > > > > you do want to force
> > > > > requests thru actions and avoid direct JSP
> > > access,
> > > > > but avoid first time
> > > > > validation. I guess Action Form could be
> > > extended to
> > > > > provide this, but I
> > > > > was hoping it would already be there - as it
> > > seems
> > > > > quite obvious and
> > > > > important. I dont think we should be encouraging
> > > > > people to go directly
> > > > > to the JSP as part of the Struts framework [just
> > > my
> > > > > 2p]
> > > > You could always have one Action class that
> > > handles
> > > > all things that are auto-validatded and another
> > > for
> > > > things that shouldn't be validated.  I'm not sure
> > > how
> > > > you could have the framework handle something that
> > > is
> > > > specific for your application unless there was a
> > > > parameter that could turn off the auto-validation
> > > for
> > > > the current request
> > > > (org.apache.struts.action.Action.VALIDATE=false).
> > > >
> > > > >
> > > > > Picking up on what you mentioned David, about
> > > > > handling all 'actions'
> > > > > todo with a form in the one Action class. We
> > > have
> > > > > some sceanrios where
> > > > > this is just not practical. What about those
> > > > > scenarios ?!
> > > > You can of course have as many actions to go with
> > > a
> > > > form as you want.  I just prefer keeping it to a
> > > > minimum.
> > > >
> > > > >
> > > > > Thanks anyway David.
> > > > >
> > > > > Any other comments ?
> > > > >
> > > > > G
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: David Winterfeldt
> > > > > [mailto:dwinterfeldt@yahoo.com]
> > > > > > Sent: 31 July 2001 16:46
> > > > > > To: struts-user@jakarta.apache.org
> > > > > > Subject: RE: Automatic Form Validation
> > > > > >
> > > > > >
> > > > > > If you have a variable called action that
> > > keeps
> > > > > track
> > > > > > of what type of action you are performing
> > > (create,
> > > > > > update, delete), you could check for this in
> > > the
> > > > > > validate method and only validate on create or
> > > > > update.
> > > > > >  The Struts example webapp has an action field
> > > to
> > > > > keep
> > > > > > track of this.  I wouldn't do that personally.
> > >
> > > > > I've
> > > > > > always done my validation from the Action so I
> > > > > could
> > > > > > use the same action for everything associated
> > > with
> > > > > a
> > > > > > form/table.  For example, delete doesn't need
> > > > > > validation or sending someone through the
> > > action
> > > > > to
> > > > > > the view for the first time (as you
> > > mentionded).
> > > > > So I
> > > > > > don't think there is anything wrong with not
> > > using
> > > > > the
> > > > > > automatic validation.  I think it just depends
> > > on
> > > > > your
> > > > > > design preference.
> > > > > >
> > > > > > David
> > > > > >
> > > > > > --- "Emaho, Ghoot" <Ghoot@PETROTECHNICS.co.uk>
> > > > > wrote:
> > > > > > > Some further details on my question:
> > > > > > >
> > > > > > > I understand that you can have the request
> > > go
> > > > > > > straight to the JSP and
> > > > > > > avoid the first time validation.
> > > > > > >
> > > > > > > This is acceptable in some circumstance, but
> > > in
> > > > > > > other circumstances you
> > > > > > > may wish to have your action do some work
> > > before
> > > > > > > presenting the page (a
> > > > > > > common requirement in more complex
> > > > > applications),
> > > > > > > and you may wish to
> > > > > > > avoid access to any JSP's directly.
> > > > > > >
> > > > > > > How then (with this extra clarification) can
> > > you
> > > > > > > prevent the behaviour
> > > > > > > described in the original posting ?
> > >
> > === message truncated ===
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Make international calls for as low as $.04/minute with
> > Yahoo! Messenger
> > http://phonecard.yahoo.com/
> >
>
>
>
>




Mime
View raw message