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.

Ted Husted

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

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.