You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Thomas Heller <th...@mx4k.com> on 2002/11/22 16:28:01 UTC

pre-populated ActionForms / suggestion [patch] for beta1.1b2

[ posted to struts-user but i just realized struts-dev might be a better
place for this]

Hi there,

I'm trying to create a form with some pre-filled which either come from a
cookie or a database. i browsed the archive and found a comment from Craig
where he says the best way to do this is by routing the specified request
into a action and populate the form there. IMHO this way would complicate
the Actions execute method unnecessary complicat cause it must have a
"action-case" for "prepopulate".

IMHO the best way to handle what i would want to do would be done like this.

1. introduce a "prepare(HttpServletRequest request)" method info
"ActionForm"
2. invoke this method in "RequestUtils.createActionForm(...)" right after
"instance.setServlet(servlet)" in line 638

inside the Forms "prepare" (populate or some other name) Method i could
prepare all fields like i want including setting values from session,
request, cookies, context, db, etc since i get the current request as a
method param.

dunno how to submit patches to struts but it would look something like this:

class: org.apache.struts.action.ActionForm
[snip]

    /**
     * Prepare the form before it gets displayed first.
     * <p>
     * The default implementation does nothing.  Subclasses can
    * override this method to insert values from a DB/Cookie into
    * the form
     *
     * @param request The servlet request we are processing
     */
    public void prepare(HttpServletRequest request) {

        ;       // Default implementation does nothing

    }
[/snip]

All forms written before this method would still work and it eases the work
of setting form values by cookies alot.

Comments welcome ...

thanks,
Thomas Heller

> Date: Tue, 25 Jun 2002 20:17:35 -0400 (EDT)
> From: Matt Barnicle <br...@barnicle.org>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Cc: struts@barnicle.org
> Subject: Re: Playing with cookies - How?
>
> Thanks again, Chris.  This helps me out a bit, but I'm still clueless on
> how to take the cookie value to pre-populate the form.  I know I could
> come up with some way to rig it, like:
>
> <html:radio name="foo" value="1"
>   <logic:equal name="mycookie" property="value"
value="1">checked="checked">
>
> But that seems to defeat the usefulness of Struts, and I'm sure there's a
> more appropriate way to do it.  How?
>

This kind of syntax actually won't work anyway -- you cannot nest an XML
tag inside an attribute value.

IMHO, the best way to deal with situations like this is the same as any
other situation where you need to pre-populate a form -- route the
incoming request through an Action that pulls out whatever you need from
your cookies and uses it to populate beans that are later used by the JSP
page that you forward to.

The struts-example application uses this design pattern (although with a
database lookup instead of cookies) when you select the "edit your
profile" option.  This is routed through the /editRegistration action,
which looks in the pseudo-database and prepopulates the form bean for the
registration form.

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: pre-populated ActionForms / suggestion [patch] for beta1.1b2

Posted by Eddie Bush <ek...@swbell.net>.
Thomas Heller wrote:

>[ posted to struts-user but i just realized struts-dev might be a better
>place for this]
>
... actually, it's a struts-user question.

>Hi there,
>
>I'm trying to create a form with some pre-filled which either come from a
>cookie or a database. i browsed the archive and found a comment from Craig
>where he says the best way to do this is by routing the specified request
>into a action and populate the form there. IMHO this way would complicate
>the Actions execute method unnecessary complicat cause it must have a
>"action-case" for "prepopulate".
>
That's the way it's done.

>IMHO the best way to handle what i would want to do would be done like this.
>
>1. introduce a "prepare(HttpServletRequest request)" method info
>"ActionForm"
>2. invoke this method in "RequestUtils.createActionForm(...)" right after
>"instance.setServlet(servlet)" in line 638
>
Forms are just a conduit by which data is transferred from the user to 
an action.  In addition, you may wish to use the same form with 
different actions (and have it contain different data).  The forms 
should be as stupid as possible - your suggestions don't allow that.  Of 
course, if you really insist on this behavior ... you have the source :-)

>inside the Forms "prepare" (populate or some other name) Method i could
>prepare all fields like i want including setting values from session,
>request, cookies, context, db, etc since i get the current request as a
>method param.
>
You can do that in your action :-) and since you can associate a form 
with many different actions you have the flexibility to populate it in 
different ways depending on the action that actually populates it ;-)

>dunno how to submit patches to struts but it would look something like this:
>
http://issues.apache.org/bugzilla <--- Our bug database.  Create a "bug" 
and then attach your code as a "cvs diff -u".

>class: org.apache.struts.action.ActionForm
>[snip]
>
>    /**
>     * Prepare the form before it gets displayed first.
>     * <p>
>     * The default implementation does nothing.  Subclasses can
>    * override this method to insert values from a DB/Cookie into
>    * the form
>     *
>     * @param request The servlet request we are processing
>     */
>    public void prepare(HttpServletRequest request) {
>
>        ;       // Default implementation does nothing
>
>    }
>[/snip]
>
>All forms written before this method would still work and it eases the work
>of setting form values by cookies alot.
>
I don't really see how it does anything but limit the use of the form. 
 You would have no additional functionality available to you in the 
proposed "prepare" method compared to what you can do in an action's 
"execute" method.  Again, if you wish to make your forms so specfic, 
there's nothing to keep you from doing it - the source if freely 
available, and there's nothing that keeps you from modifying it to suit 
your own needs.

>Comments welcome ...
>
>thanks,
>Thomas Heller
>

-- 
Eddie Bush





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: pre-populated ActionForms / suggestion [patch] for beta1.1b2

Posted by Thomas Heller <th...@mx4k.com>.
Hi,

> I agree with Eddie and Martin that this is probably not a good idea, but
> for a different reason -- it would violate the layering of the MVC model.
>
> Remember that form beans are part of the View tier.  Adding a prepare()
> method for the purposes you propose would typically involve interacting
> with the model (getting stuff from the database, applying business rules
> to decide what information to display, and so on.  That kind of thing
> should really be done via an Action, which is why Struts encourages the
> current approach.
>
> > --
> > Martin Cooper
>
> Craig

I partly agree with you. I agree that forms should be inside the View tier,
but IMHO the methods "reset" and "validate" are methods of
business/controller logic.

Ideally i have our webdesigners create all the forms as _very_ simple beans
(just some getters and setters). wether the the received input is valid
shouldnt be decided/checked by them instead i (controller) should do it.

For example if i look at the current implementation of the
struts-example.war I find an issue that should point out what i mean.

If u take RegistrationForm.java and SaveRegistrationAction.java for example
the forms "validate" method does a simple check wether both entered
passwords match. now later in the Action there are some more tests (username
unique, etc) and perhaps another ActionErrors instance is created. We now
did a form-validate (Form) and the action again does some "kind-of"
form-validation. maybe my suggested "prepare" method and the "reset",
"validate" method would fit better into the "Action" class as "prepareForm",
"resetForm", "validateForm".

Thomas


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: pre-populated ActionForms / suggestion [patch] for beta1.1b2

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 22 Nov 2002, Martin Cooper wrote:

> Date: Fri, 22 Nov 2002 09:08:36 -0800 (PST)
> From: Martin Cooper <ma...@apache.org>
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: Struts Developers List <st...@jakarta.apache.org>
> Subject: Re: pre-populated ActionForms / suggestion [patch] for beta1.1b2
>
>
>
> On Fri, 22 Nov 2002, Thomas Heller wrote:
>
> > [ posted to struts-user but i just realized struts-dev might be a better
> > place for this]
> >
> > Hi there,
> >
> > I'm trying to create a form with some pre-filled which either come from a
> > cookie or a database. i browsed the archive and found a comment from Craig
> > where he says the best way to do this is by routing the specified request
> > into a action and populate the form there. IMHO this way would complicate
> > the Actions execute method unnecessary complicat cause it must have a
> > "action-case" for "prepopulate".
> >
> > IMHO the best way to handle what i would want to do would be done like this.
> >
> > 1. introduce a "prepare(HttpServletRequest request)" method info
> > "ActionForm"
> > 2. invoke this method in "RequestUtils.createActionForm(...)" right after
> > "instance.setServlet(servlet)" in line 638
>
> I agree with Eddie that you really should be doing this work in an action.
> Even if you did have a prepare() method, this certainly isn't where you
> would want it to be called, and there's nowhere else Struts could call it
> that would make sense.
>
> The problem with calling it here is that I really don't think you want to
> be going through all the trouble of filling out the form fields right
> before Struts overwrites them with the parameters of an incoming request.
> Remember that form beans are created for you when a request comes in, and
> your prepare() method would be called then too.
>

I agree with Eddie and Martin that this is probably not a good idea, but
for a different reason -- it would violate the layering of the MVC model.

Remember that form beans are part of the View tier.  Adding a prepare()
method for the purposes you propose would typically involve interacting
with the model (getting stuff from the database, applying business rules
to decide what information to display, and so on.  That kind of thing
should really be done via an Action, which is why Struts encourages the
current approach.

> --
> Martin Cooper

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: pre-populated ActionForms / suggestion [patch] for beta1.1b2

Posted by Martin Cooper <ma...@apache.org>.

On Fri, 22 Nov 2002, Thomas Heller wrote:

> [ posted to struts-user but i just realized struts-dev might be a better
> place for this]
>
> Hi there,
>
> I'm trying to create a form with some pre-filled which either come from a
> cookie or a database. i browsed the archive and found a comment from Craig
> where he says the best way to do this is by routing the specified request
> into a action and populate the form there. IMHO this way would complicate
> the Actions execute method unnecessary complicat cause it must have a
> "action-case" for "prepopulate".
>
> IMHO the best way to handle what i would want to do would be done like this.
>
> 1. introduce a "prepare(HttpServletRequest request)" method info
> "ActionForm"
> 2. invoke this method in "RequestUtils.createActionForm(...)" right after
> "instance.setServlet(servlet)" in line 638

I agree with Eddie that you really should be doing this work in an action.
Even if you did have a prepare() method, this certainly isn't where you
would want it to be called, and there's nowhere else Struts could call it
that would make sense.

The problem with calling it here is that I really don't think you want to
be going through all the trouble of filling out the form fields right
before Struts overwrites them with the parameters of an incoming request.
Remember that form beans are created for you when a request comes in, and
your prepare() method would be called then too.

--
Martin Cooper


>
> inside the Forms "prepare" (populate or some other name) Method i could
> prepare all fields like i want including setting values from session,
> request, cookies, context, db, etc since i get the current request as a
> method param.
>
> dunno how to submit patches to struts but it would look something like this:
>
> class: org.apache.struts.action.ActionForm
> [snip]
>
>     /**
>      * Prepare the form before it gets displayed first.
>      * <p>
>      * The default implementation does nothing.  Subclasses can
>     * override this method to insert values from a DB/Cookie into
>     * the form
>      *
>      * @param request The servlet request we are processing
>      */
>     public void prepare(HttpServletRequest request) {
>
>         ;       // Default implementation does nothing
>
>     }
> [/snip]
>
> All forms written before this method would still work and it eases the work
> of setting form values by cookies alot.
>
> Comments welcome ...
>
> thanks,
> Thomas Heller
>
> > Date: Tue, 25 Jun 2002 20:17:35 -0400 (EDT)
> > From: Matt Barnicle <br...@barnicle.org>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: Struts Users Mailing List <st...@jakarta.apache.org>
> > Cc: struts@barnicle.org
> > Subject: Re: Playing with cookies - How?
> >
> > Thanks again, Chris.  This helps me out a bit, but I'm still clueless on
> > how to take the cookie value to pre-populate the form.  I know I could
> > come up with some way to rig it, like:
> >
> > <html:radio name="foo" value="1"
> >   <logic:equal name="mycookie" property="value"
> value="1">checked="checked">
> >
> > But that seems to defeat the usefulness of Struts, and I'm sure there's a
> > more appropriate way to do it.  How?
> >
>
> This kind of syntax actually won't work anyway -- you cannot nest an XML
> tag inside an attribute value.
>
> IMHO, the best way to deal with situations like this is the same as any
> other situation where you need to pre-populate a form -- route the
> incoming request through an Action that pulls out whatever you need from
> your cookies and uses it to populate beans that are later used by the JSP
> page that you forward to.
>
> The struts-example application uses this design pattern (although with a
> database lookup instead of cookies) when you select the "edit your
> profile" option.  This is routed through the /editRegistration action,
> which looks in the pseudo-database and prepopulates the form bean for the
> registration form.
>
> Craig
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>