You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Martin Cooper <ma...@tumbleweed.com> on 2002/11/12 03:40:18 UTC

RE: [SIDEBAR] Form population (Was RE: request.setAttribute() for m confusion)


> -----Original Message-----
> From: Eddie Bush [mailto:ekbush@swbell.net]
> Sent: Monday, November 11, 2002 2:46 PM
> To: Struts Users Mailing List
> Subject: Re: [SIDEBAR] Form population (Was RE: request.setAttribute()
> form confusion)
> 
> 
> Sri, pardon me, but I don't see how what you've said has 
> anything to do 
> with the point I was making.  My sole point is that the 
> actual creation 
> (instantiation) of the form bean itself is a very mechanical act that 
> Struts can perform on your behalf.  There is no need to ever 
> bother with 
> creating a bean and placing it into some scope - just 
> configure things 
> properly and let it be done for you.  Yes, you may very well need to 
> initialize it's values in an action - but that is a seperate act from 
> instantiation.

I think you missed the point here, Eddie (or perhaps I missed that you
didn't miss the point :).

Think about it this way. Struts will create your form bean automagically in
a couple of cases: when it needs to populate one from an incoming request,
and when it needs to render a form but can't find the corresponding form
bean.

Now consider the case where you need to populate the form with some values
that the Action came up with somehow. You'd do that by having the values in
the form bean properties, right? That way, the Struts HTML tags will do the
work for you.

But now consider the case where you haven't yet invoked anything in the app
that would cause the appropriate form bean to be instantiated. The action
that came up with the values might have been invoked with a different form
bean, or perhaps no form bean at all. So Struts hasn't instantiated the bean
yet. Struts *will* instantiate the bean when it the <html:form> tag is
processed, but by then, it's too late for the Action to populate the form
bean.

So the only way for the action to pre-populate the form bean, so that those
values are picked up by the Struts HTML tags to populate the form is for the
action to instantiate the form bean itself.

One way of creating the form bean is RequestUtils.createActionForm().

--
Martin Cooper


> 
> Why bother specifying a form-bean in your config file if 
> you're going to 
> create it yourself?  Yeah there's some validation that can be 
> done on it 
> by Struts - but if you don't specify a form-bean for an 
> action-mapping 
> (you specify nothing for the action-mapping's name attribute) I don't 
> believe that validation is done (why would it be?  Struts 
> doesn't know 
> there's a form being used for this action!).  Hrm - in fact 
> you lose a 
> great deal of the utility Struts provides if you don't make this 
> association.  Think about it:  You have to tell Struts what type your 
> form is of - so it can instantiate it.  It needs this 
> information for no 
> other purpose.  It could easily tell later on that the form is of the 
> wrong type for validation etc.  Also, you could specify by 
> the attribute 
> property the name of a form which you instantiated yourself.  You 
> *could* (if you wanted to) cut yourself off from the Struts form 
> mechanism altogether.
> 
> What I suggest in no way impacts your ability to use good design 
> (delegates etc).  It just takes one very mechanical thing out of your 
> hands - and out of your concern - and "just does it".  It's 
> solid.  It 
> works.  If it ever fails you *please* file a bug!  It also 
> has no impact 
> on where data comes from or where it goes to.  You can pass 
> data via a 
> form from one action to another in about 2 ways that come to mind 
> immediately:  1) session-scoped form 2) using hidden fields.  
> I'm sorry, 
> but I think you misunderstood my entire argument.  My 
> appologies for not 
> having been clearer.
> 
> Sri Sankaran wrote:
> 
> >While that is true, other than in the case of blank forms, 
> isn't it true that one rarely depends on Struts to 
> auto-generate the form bean?  If you are presenting data, the 
> form is pre-populated with such data.  This data is typically 
> derived by an earlier action -- mediated by a business delegate.  
> >
> >Say you have action-1 which is invoked when a page-1 is 
> submitted.  It does sundry business functions which includes 
> getting the data necessary for the next page, page-2 say.  In 
> order for page-2 to pick up this data, action-1 must know the 
> 'name' attribute for the action-mapping corresponding to 
> page-2 and save the form-bean under this name in the 
> appropriate scope.
> >
> >Is there a way around this issue?
> >
> >Sri
> >-----Original Message-----
> >From: Eddie Bush [mailto:ekbush@swbell.net] 
> >Sent: Monday, November 11, 2002 3:00 PM
> >To: Struts Users Mailing List
> >Subject: Re: request.setAttribute() form confusion
> >
> >
> >You shouldn't ever have to create the form yourself if you:
> >
> >- declare the form-bean in your config file
> >- use the name of the form you declared in the action 
> mapping's "name" 
> >attribute
> >
> ><form-bean name="myForm" ... />
> >
> >...
> >
> ><action path="/somePath" name="myForm" ... />
> >
> >That should suffice.  What you should find is that the form 
> is created 
> >for you.  Doing things as I mention simplifies you actions 
> so that they 
> >can just expect the form to be there.  If the form is not 
> there you will 
> >probably NPE.  That would indicate that you didn't create the 
> >association (by setting name="myForm") or Struts wasn't able 
> to create 
> >the form for some odd reason.
> >
> -- 
> Eddie Bush
> 
> 
> 
> 
> --
> 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>


Re: [SIDEBAR] Form population (Was RE: request.setAttribute() for m confusion)

Posted by Eddie Bush <ek...@swbell.net>.
Martin Cooper wrote:

>>-----Original Message-----
>>From: Eddie Bush [mailto:ekbush@swbell.net]
>>Sent: Monday, November 11, 2002 2:46 PM
>>To: Struts Users Mailing List
>>Subject: Re: [SIDEBAR] Form population (Was RE: request.setAttribute()
>>form confusion)
>>
>>
>>Sri, pardon me, but I don't see how what you've said has 
>>anything to do 
>>with the point I was making.  My sole point is that the 
>>actual creation 
>>(instantiation) of the form bean itself is a very mechanical act that 
>>Struts can perform on your behalf.  There is no need to ever 
>>bother with 
>>creating a bean and placing it into some scope - just 
>>configure things 
>>properly and let it be done for you.  Yes, you may very well need to 
>>initialize it's values in an action - but that is a seperate act from 
>>instantiation.
>>    
>>
>I think you missed the point here, Eddie (or perhaps I missed that you
>didn't miss the point :).
>
>Think about it this way. Struts will create your form bean automagically in
>a couple of cases: when it needs to populate one from an incoming request,
>and when it needs to render a form but can't find the corresponding form
>bean.
>
>Now consider the case where you need to populate the form with some values
>that the Action came up with somehow. You'd do that by having the values in
>the form bean properties, right? That way, the Struts HTML tags will do the
>work for you.
>
>But now consider the case where you haven't yet invoked anything in the app
>that would cause the appropriate form bean to be instantiated. The action
>that came up with the values might have been invoked with a different form
>bean, or perhaps no form bean at all. So Struts hasn't instantiated the bean
>yet. Struts *will* instantiate the bean when it the <html:form> tag is
>processed, but by then, it's too late for the Action to populate the form
>bean.
>
Yes, but at this point it's not terribly important - assuming the submit 
of this form is the first time the form is really needed (for capturing 
values).  I was just trying to empasize why a person wanted to trust 
Struts to do this for them - not cover all bases of usage ;-)

>So the only way for the action to pre-populate the form bean, so that those
>values are picked up by the Struts HTML tags to populate the form is for the
>action to instantiate the form bean itself.
>
Huh?  Ok I think I missed something here.  I've always had my form 
available to me if I told Struts an action used it.  If you're not 
pre-populating (for display) it's really not important to have it 
created ahead of time.  You're guaranteed the form will be there to 
capture any submit as we process the associated form (read:  we check 
for an existing form and instantiate one if it does not exist) before we 
populate.  One of us is confused.  My bet is that you misinterpreted 
what I was getting at.  Again, my sole point was that Struts will create 
forms for a person if they simply connect the dots between the form and 
the action so that Struts is aware that it needs to be done.

>One way of creating the form bean is RequestUtils.createActionForm().
>
>--
>Martin Cooper
>
-- 
Eddie Bush




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