Introduction of dynamic form
Although Actionform is powerful, it is sometimes troublesome to use. For each user input interface (JSP page), you have to create a actionform subclass to correspond to this page. Of course, we can use nested attributes to some extent to remedy this problem. However, when creating new user input interface, it is unavoidable to establish new Actionform subclass.
A technique for dynamic form is provided in the Struts1.2.6 and later struts versions. This makes it possible to encapsulate data submitted by users without having to create new actionform. In fact, the technique of defining the Actionform subclass becomes the work of writing XML files.
Each dynamic form is defined by adding a <form-bean> element to the Struts-config.xml and using <form-property> child elements to define the properties of the dynamic form.
At the end of this chapter, we'll introduce a Lazyvalidatorform class that allows you to use dynamic form even without defining the properties of the dynamic form. This will greatly simplify the developer's workload.
Ii. Declaration of dynamic form
Declaring a dynamic form is simply a matter of adding a <form-bean> child element to the Struts-config.xml <form-beans> element and using the <form-property> element to define the properties of the dynamic form. We can define attribute types with simple attributes such as String, indexed properties (such as arrays), mapped attributes (such as HASHMAP), and nested properties (property types are another class).
For a dynamic form, the type of form must be org.apache.struts.action.DynaActionForm or its subclasses. The code that declares a dynamic form is as follows:
<form-bean name= "Dynamicform" type= "Org.apache.struts.action.DynaActionForm" >
<!--declare a simple property-->
<form-property name= "Simpleprop" type= "java.lang.String" initial= "Bill"/>
<!--declare an index property-->
<form-property name= "Indexedprop" type= "java.lang.string["/>
<!--declare a mapping attribute-->
<form-property name= "Mappedprop" type= "Java.util.HashMap"/>
<!--declare a nested property-->
<form-property name= "Nestedprop" type= "Com.bean.MyBean"/>
</form-bean>
Three, the dynamic form of the property type
All types of simple properties supported by dynamic form are listed below:
Java.math.BigDecimal
Java.math.BigInteger
Java.lang.Boolean
Java.lang.Byte
Java.lang.Character
Java.lang.Class
Java.lang.Double
Java.lang.Float
Java.lang.Integer
Java.lang.Long
Java.lang.Short
Java.lang.String
Java.sql.Date
Java.sql.Time
Java.sql.Timestamp
In fact, the data type above is the simple data type provided in Java. They are exactly the same as the data types represented in the dynamic form and in Java. We can also use the initial property of the <form-property> element to specify a default value for the simple property of the dynamic form. As in routine 6-11, the default value for the Name property is "Bill."
For the type of indexed property, it can be an array or a class of java.util.List interfaces (such as ArrayList). We can also specify the class that implements the Java.util.Map interface as the data type (that is, the mapping attribute) for the properties of the dynamic form. Unfortunately, if you use dynamic form, you cannot use generics for automatic type conversions.