5.2. Setting DynaActionForm Initial Values

Problem

You need to initialize the properties in a dynamic action form declared in the struts-config.xml file.

Solution

Specify an initial value for a property of a DynaActionForm using the initial attribute of the form-property element.

<form-bean name="MyForm" type="org.apache.struts.action.DynaActionForm">
  <form-property name="firstName" type="java.lang.String"
              initial="George"/>
  <form-property name="lastName" type="java.lang.String"
              initial="Burdell"/>
  <form-property name="javaCoder" type="java.lang.Boolean"
              initial="true"/>
  <form-property name="friend" type="java.lang.String[]"
                 size="3" 
              initial="Larry,Moe,Curly"/>
</form-bean>

Discussion

DynaActionForms simplify your life as a Struts developer. There is no Java code to write, and when requirements change and you must add fields to a form, you need only change some XML and the Actions that use the form. When working with these forms, a common question that comes up is how to populate the form with initial values. The Solution shows how this can be done using the initial attribute of the form-property element. This attribute specifies a default value for a property. The property is set to this value when the form is created or when the initialize( ) method is called on the form. The time of creation depends on the scope of the form. If the scope is "request," a new form is created for each new request that uses the form. For session scope, the form is created when it can't be found in the ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.