I have a web app where users are allowed to edit data stored in a DB. In many cases, they arrive at a page with a form appearing there already filled out with values from the DB.
In the past I always used ordinary ActionForms for this type of situation. Set the fields in an Action and then use the Struts html tags to implement the form, so the values would show up there.
However, for the current project, we're looking at using DynaActionForms. I have searched these forums and done a Google search, but strangely, I can't find any info on how to prepopulate the fields of a DynaActionForm. Is it not normal to do that?
Any help to get in the right direction would be greatly appreciated.
Joined: Feb 15, 2005
You can certainly prepopulate a DynaActionForm. There are a couple of conditions, though. One is that you can't instantiate a DynaActionForm with new DynaActionForm(). You have to associate the action that prepopulates the form with the form bean in the Action mapping. Then, when the Action's execute() method is called, struts instantiates the DynaActionForm and passes it in as a parameter. Since the parameter comes in as type "ActionForm", you have to cast it to "DynaActionForm" so that you can use it.
Once you had the instance, you can call the set() method to populate any variables. (Example: myDynaActionForm.set("lastName", "Smith"))