The Life of An ActionForm
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mark_Bennett
Posted On:   Sunday, March 31, 2002 07:03 PM

Hi, I'm trying to setup a form that will populate with the information about a user (their name, address, etc.) I'm having trouble with this because I don't really understand how the ActionFormBeans work. When are they created and destroyed? When is the reset() method called? Should I call it myself or does Struts do it for me? What about the validate() method? Is it only triggered when I call it in my code? I have seen some forms that create the ActionForm bean in one action that creates the populated form page, and then uses another action to store the data in the model. How do I set this up? Will the reset() method not be called before I want it to? If someone could please clear   More>>

Hi,


I'm trying to setup a form that will populate with the information about a user (their name, address, etc.) I'm having trouble with this because I don't really understand how the ActionFormBeans work.



When are they created and destroyed? When is the reset() method called? Should I call it myself or does Struts do it for me? What about the validate() method? Is it only triggered when I call it in my code?



I have seen some forms that create the ActionForm bean in one action that creates the populated form page, and then uses another action to store the data in the model. How do I set this up? Will the reset() method not be called before I want it to?



If someone could please clear up the life cycle of an ActionForm bean I would appreciate it. Thanks!

   <<Less

Re: The Life of An ActionForm

Posted By:   Ted_Husted  
Posted On:   Saturday, April 6, 2002 06:16 AM


Struts uses ActionForms to both populate a HTML form, and then to capture the parameters
when a form is submitted.



When a request is submitted, the ActionServlet matches the URI
for the request against the path of an action-mapping. If there is a form-bean for that
action-mapping, it looks for an ActionForm under the form-bean name. If it doesn't find
one, a new one is created. The ActionServlet then calls reset() on the new or pre-existing
ActionForm and populates it from the request. If the action-mapping has validate
set to true, the ActionServlet calls the validate method. If this returns any error messages,
the ActionServlet forwards to the URI indicated by the action-mappings error property. This
is often a JSP, but it can also be another action-mapping. The latter being needed when
there are drop-down controls to populate and so forth. If there are no errors, the ActionServlet
passes the (populated and validated) ActionForm to the Action indicated by the
action-mapping element.



When a JSP with an html:form tag is rendered, the html:form tag uses its action path to
look-up its action-mapping. Like the ActionServlet, it uses the form-bean to check for a
ActionForm in the appropriate servlet context. If it doesn't find one, it creates one. The
new or pre-existing ActionForm is then used by the other html:tags to populate their
elements.



It's important to note that the form-bean name is used as the default attribute name.
Another attribute name can be specified in the action-mapping if needed. Likewise, the
html tags default to using the ActionForm related to the html:form tag, but any bean
can be specified for any html tag element (ActionForm or not).



When passing ActionForm beans between Actions it is important to note that the ActionServlet
will try to populate it again between Actions. If you modify a value, you must be sure to
protect it from autopopulate or reset. One way to do that is to add a mutable switch
to your ActionForm bean. See
SuperForm
for an example



HTH, Ted

About | Sitemap | Contact