You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Susan Bradeen <Su...@softlanding.com> on 2002/11/11 19:34:16 UTC

request.setAttribute() form confusion

If I have this code, which appears to be good practice, in the beginning 
of my pre-action's execute() method:

   if (form == null) {
      form = new SomeForm();
      if ("request".equals(mapping.getScope())) {
         request.setAttribute(mapping.getAttribute(), form);
      }
      else {
         request.getSession().setAttribute(mapping.getAttribute(), form);
      }
   }
   SomeForm myForm = (SomeForm)form;
   /* populate form values ...  */

Do I also need to use the following line at the end (after form 
population), or is this basically accomplishing the same as the above? 

   request.setAttribute(mapping.getAttribute(), myForm);


Thanks,
Susan Bradeen

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


Re: request.setAttribute() form confusion

Posted by Kris Schneider <kr...@dotech.com>.
Susan,

Here's a response to a different thread with the same type of code block:

http://marc.theaimsgroup.com/?l=struts-user&m=103669042429912&w=2

I understand this type of approach is used in the example app, but I'd say it's 
anything but good practice. In general, if you have an action that's going to 
pre-populate a form, it's action mapping should include a name (or attribute) 
attribute for the appropriate form. Struts will take care of finding or 
creating the form, ensuring that it's stored in the proper scope, and passing 
it to the execute/perform method. All you need to do then is cast it to the 
appropriate type.

Quoting Susan Bradeen <Su...@softlanding.com>:

> If I have this code, which appears to be good practice, in the beginning 
> of my pre-action's execute() method:
> 
>    if (form == null) {
>       form = new SomeForm();
>       if ("request".equals(mapping.getScope())) {
>          request.setAttribute(mapping.getAttribute(), form);
>       }
>       else {
>          request.getSession().setAttribute(mapping.getAttribute(), form);
>       }
>    }
>    SomeForm myForm = (SomeForm)form;
>    /* populate form values ...  */
> 
> Do I also need to use the following line at the end (after form 
> population), or is this basically accomplishing the same as the above? 
> 
>    request.setAttribute(mapping.getAttribute(), myForm);
> 
> 
> Thanks,
> Susan Bradeen
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

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