How to prepopulate a form in Struts

By: Apache Foundation Emailed: 1744 times Printed: 2432 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The simplest way to prepopulate a form is to have an Action 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 Action 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 struts-config.xml file:

        ...
        <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 /editRegistration and /saveRegistration 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 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 ActionForm that points at the page which will display this form. If you are using the Struts JSP tag library, the action attribute on your <html:form> tag will be set to /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 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 ActionForward named "registration" in this case, because the example webapp uses the inputForward attribute in the <controller> element -- see the documentation describing struts-config.xml for more information) instead of calling your processing action.

Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Thanks for the info

View Tutorial          By: info at 2010-01-31 12:17:58
2. View Comment

Where should I call the action /editRegistration?
I have a login form (jsp) with LoginForm and LoginAction and if login is successful I redirect SUCCESS to a userpage that i want to be prepopulated.


View Tutorial          By: Andrei at 2013-03-08 12:21:07

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Apache Foundation
When is the best time to validate input in Struts
Chaining actions in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Struts 1 vs Struts 2
ActionErrors and ActionError in Struts
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
ForwardAction in Struts
Configuring JDBC DataSources in Struts
Handling multiple buttons in HTML Form in Struts
IncludeAction in Struts
Using JavaScript to submit a form in Struts
MVC Architecture (Model 2 Architecture)
Struts and Tiles - Steps to use Struts and Tiles
FAQ: Why are my checkboxes not being set from ON to OFF?
Editing struts-config.xml in a Struts Application
Introduction to Struts Architecture
Most Emailed Articles (in Struts)
Guidelines for Struts Application Development
WebWork 2 is now Apache Struts 2
Downloading and installing Struts
Struts 1 vs Struts 2
The directories and files of a Struts application
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
Is Struts the most popular web application framework for Java?
What is a Model View Controller (MVC) Model?
History of Struts
Creating Struts Modules
Using Multibox in Struts
ForwardAction in Struts
What is Struts? Which Version of Struts to use?
Editing web.xml in a Struts Application