The simplest way to prepopulate a form is to have an
whose sole purpose is to populate an
ActionForm and forward to the
servlet or JSP to render that form back to the client. A separate
would then be use to process the submitted form fields, by declaring an instance
of the same form bean name.
The struts-example example application that is shipped with Struts
illustrates this design pattern nicely. Note the following definitions from the
<-- Registration form bean -->
<-- Edit user registration -->
<-- Save user registration -->
Note the following features of this approach:
- Both the
actions use the same form bean.
- When the
/editRegistration action is entered, Struts will
have pre-created an empty form bean instance, and passed it to the
method. The setup action is free to preconfigure the values that will be
displayed when the form is rendered, simply by setting the corresponding
form bean properties.
- When the setup action completes configuring the properties of the form
bean, it should return an
ActionForm that points at the page
which will display this form. If you are using the Struts JSP tag library,
action attribute on your <html:form> tag will be set
/saveRegistration in order for the form to be submitted to
the processing action.
- Note that the setup action (
/editRegistration) turns off
validation on the form that is being set up. You will normally want to
include this attribute in the configuration of your setup actions, because
you are not planning to actually process the results -- you simply want to
take advantage of the fact that Struts will precreate a form bean instance
of the correct class for you.
- The processing action (
/saveRegistration), on the other hand,
leaves out the
validate attribute, which defaults to
This tells Struts to perform the validations associated with this form bean
before invoking the processing action at all. If any validation errors have
occurred, Struts will forward back to your input page (technically, it
forwards back to an
"registration" in this case, because the example webapp uses the
attribute in the
<controller> element -- see the
struts-config.xml for more
information) instead of calling your processing action.