How to prepopulate a form in Struts
By: Apache Foundation in Struts Tutorials on 2008-09-20
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
... <form-beans> ... <-- Registration form bean --> <form-bean name="registrationForm" type="org.apache.struts.webapp.example.RegistrationForm"/> ... </form-beans> ... <action-mappings> ... <-- Edit user registration --> <action path="/editRegistration" type="org.apache.struts.webapp.example.EditRegistrationAction" name="registrationForm" scope="request" validate="false"/> ... <-- Save user registration --> <action path="/saveRegistration" type="org.apache.struts.webapp.example.SaveRegistrationAction" name="registrationForm" input="registration" scope="request"/> ... </action-mappings>
Note the following features of this approach:
- Both the
/saveRegistrationactions use the same form bean.
- When the
/editRegistrationaction is entered, Struts will have pre-created an empty form bean instance, and passed it to the
execute()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
ActionFormthat points at the page which will display this form. If you are using the Struts JSP tag library, the
actionattribute on your <html:form> tag will be set to
/saveRegistrationin 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
validateattribute, which defaults to
true. 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
ActionForwardnamed "registration" in this case, because the example webapp uses the
inputForwardattribute in the
<controller>element -- see the documentation describing
struts-config.xmlfor more information) instead of calling your processing action.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Handling Duplicate Form Submissions in Struts
Guidelines for Struts Application Development
Configuring JDBC DataSources in Struts
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
How to prepopulate a form in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
What is a Plug-in and how to use Java plug-ins with Struts?