Dynamic actionform of actionform
In the Struts framework, the actionform object is used to wrap HTML form data and dynamically return data for display to users. The custom actionform must comply with the JavaBean specification and inherit the struts actionform class, at the same time, you can override two methods: reset () and validate ().
The only drawback of actionform is that for large-scale struts applications, a large number of actionform classes must be created programmatically. If the HTML form field changes, the related actionform classes must be modified and compiled. Struts1.1 has been improved to introduce the concept of the dynamic actionform class. The dynaactionform class of the Struts framework and its subclass implement the dynamic actionform, which is a subclass of the actionform class. Dynamic actionform makes it unnecessary for users to applyProgramCreate a specific actionform class for each HTML form. The dynaactionform class in the Struts Framework Package and its subclass implement dynamic actionform. The dynaactionform class extends the actionform class. You can directly use the dynaactionform class, or extend the class to overwrite its validate and reset methods! The principle of dynamic actionarom data collection is: there is a map in the dynamic actionform, which is collected using map. The key value is the name of the form, and the value is the value corresponding to the form, using the complete form of the type, he places the data in the form in the map, so the data is similar to the data in the map. Note that the retrieved value is of the object type, and type conversion is required.
1. Configure dynamic actionform
Dynamic actionform supports complete actionform configuration in the struts configuration file. There is no need to write additional programs to create a specific actionform class. The dynamic actionform configuration method is as follows: configure a <form-bean> element in the struts configuration file and set the type attribute to the full name of dynaactionform or a subclass of dynaactionform. For example:
<Form-beans>
<Form-bean
Name = "loginform" // name yourself
Type = "org. Apache. Struts. Action. dynaactionform">
<Form-property // configure attributes. All attributes to be collected need to be configured. Here, an initial attribute can be initialized. There is also a size attribute, the length of the index group, which is used when an array is declared. Dynamic verification is used for validater verification.
Name = "email"
Type = "Java. Lang. String"/> // type write complete
<Form-Property
Name = "password"
Type = "Java. Lang. String"/>
<Form-Property
Initial = "false" // set the Initial Value
Name = "rememberme"
Type = "Java. Lang. boolean"/>
</Form-bean>
</Form-beans>
The <form-property> sub-element of <form-bean> is used to set the attributes of dynamic actionform. The <form-property> element's name attribute specifies the attribute name and type specifies the attribute type, you can set the dynamic actionform attribute to the following Java type:
- Java. Lang. bigdecimal
- Java. Lang. 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
If the field value of the form is of the Java basic type, it should be replaced by the corresponding packaging type during configuration. For example, the int type is of the integer type:
<Form-Property
Initial = "0"
Name = "Age"
Type = "Java. Lang. Integer"/>
2. Reset () method of dynamic actionform
The dynaactionform base class provides the initialize () method, which restores all attributes of the form to the default value. The default value of form attributes is determined by the initial attribute of the <form-property> sub-element of <form-bean>. If the initial attribute is not set, the default value of the form attribute is automatically determined by its Java type.
The initialize () method of the dynaactionform base class is as follows:
Public void initialize (actionmapping mapping ){
String name = mapping. getname ();
If (name = NULL ){
Return;
}
Formbeanconfig Config =
Mapping. getmoduleconfig (). findformbeanconfig (name );
If (Config = NULL ){
Return;
}
Formpropertyconfig props [] = config. findformpropertyconfigs ();
For (INT I = 0; I <props. length; I ++ ){
Set (props [I]. getname (), props [I]. Initial ());
}
}
The reset () method of the dynaactionform base class does not perform any operations,CodeAs follows:
Public void reset (actionmapping mapping, httpservletrequest request ){
}
If you want the Struts framework to restore all attributes to the default value before assembling form data into the dynamic actionform, you can define a subclass of the extended dynaactionform class and overwrite its reset () method. In the reset method, you only need to call the initialize () method.
3. Access dynamic actionform
The action class and JSP can both access the dynamic actionform. The usage is roughly the same as that of the standard actionform. There is only one small difference. If the standard actionform object is used, the get/set method is provided for each attribute in the standard actionform to read or set the attribute.
Dynaactionform stores all attributes in a map class object, and provides the following general methods for accessing all attributes:
Public object get (string name );
Public void set (string name, object value );
The get (string name) method returns the property value based on the specified property name. The set (string name, object Value) method is used to assign values to a given property. For example, if you access the email attribute in the dynaactionform class, you can use:
// Get email
String email = (string) form. Get ("email"); // The object type is returned. type conversion is required during retrieval.
// Set email
Form. Set ("email", example@example.com );
4. Form Verification of dynamic actionform
The Validate () method of the dynaactionform base class does not provide any default verification behavior. You can define the subclass of the extended dynaactionform and then override the validate () method, however, dynamic actionform verification by programming violates the original intention of Struts framework to provide dynamic actionform, that is, it replaces programming by configuration. Another verification mechanism can be used, that is, the validator framework is used to complete verification. This framework allows a specific configuration file to configure verification rules for dynamic actionform.
5. Use Dynamic actionform in applications
Call method:
<Body>
User name: $ {DYNAFORM. Map. Username} <br>
Age: $ {DYNAFORM. Map. Age} <br>
</Body>
A dynamic actionform named "itemdetailform" is defined in the application. It contains an attribute view of the itemdetailview type,
<Form-bean
Name = "itemdetailform"
Dynamic = "true"
Type = "org. Apache. Struts. Action. dynaactionform">
<Form-property name = "View" type = "netstore. Catalog. View. itemdetailview"/>
</Form-bean>
If a product is selected on the JSP page, the request is forwarded to getitemdetailaction. The configuration is as follows:
Path = "/viewitemdetail"
name = "itemdetailform"
input = "/index. JSP "
type =" netstore. catalog. getitemdetailaction "
scope =" request "
validate =" false ">
key =" Global. error. invalidlogin "
Path ="/index. JSP "
scope =" request "
type =" netstore. framework. exceptions. datastoreexception "/>
getitemdetailaction: Call the Business Method of the model based on the user-selected product ID, retrieve the detailed information of the product, and store it in the itemdetailview object, assign the itemdetailview object to the view attribute of the dynamic itemdetailform. The code for the execute () method of getitemdetailaction is as follows:
Public actionforward execute (actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
// get the primary key of the item from the request
string Itemid = request. getparameter (iconstants. id_key);
// Call the netstore service and ask it for an itemview for the item
Inetstoreservice serviceimpl = getnetstoreservice ();
Itemdetailview = serviceimpl. getitemdetailview (Itemid );
// Set the returned itemview into the dynamic action form
// The parameter name 'view' is what is defined in the Struts-config
(Dynaactionform) Form). Set ("View", itemdetailview );
// String username = (dynaactionform) Form). Get ("username ");
// (Dynaactionform) Form). Set ("password", password );
// Return the actionforward that is defined for the success condition
Return Mapping. findforward (iconstants. success_key );
}
Getitemdetailaction finally forwards the request to itemdetail. jsp, which outputs the data contained in the view attribute of the dynamic itemdetailform to the webpage through the <Bean: Write> tag,
<Bean: write name = "itemdetailform" property = "view. Name"/> <br>
</B> </font> <font size = 2>
<! -- Product model --> <Bean: Message key = "itemdetail. model "/> <Bean: write name =" itemdetailform "property =" view. modelnumber "/> <br> <! --/Product model --> </font>
<Font size = 2>
<! -- Product description -->
<Bean: write name = "itemdetailform" property = "view. Description"/>