Using actionform can greatly help developers in processing HTML forms, which simplifies Code And provides automatic data verification. However, actionform also has several obvious disadvantages.
First, if the HTML form corresponding to actionform adds or deletes some attributes, the actionform class may need to be modified and re-compiled;
Secondly, if the HTML form is dynamic, actionform is powerless;
In addition, actionform adds the absolute number of classes in the project!
Since struts1.1, the concept of dynamic actionform (dyanactionform) has been introduced. Dynamic actionform makes it unnecessary for users to apply Program Create a specific actionform class for each HTML form. The dyanactionform class in the Struts Framework Package and its subclass implement dynamic actionform.
The dyanactionform class extends the actionform class. You can directly use dyanactionform
Class, or extend the class to overwrite its validate and reset methods!
Configure dynamic actionform
Dynamic actionform supports complete actionform configuration in the struts configuration file, no need to write
Any additional Java code. The following is an example: <! -- Dynamic form demonstration -->
< Form-bean Name = "Dynaformform" Type = "Org. Apache. Struts. Action. dynaactionform" >
< Form-Property Name = "Username" Type = "Java. Lang. String" />
< Form-Property Name = "Userpwd" Type = "Java. Lang. String" />
</ Form-bean >
In the above Code, the type attribute of <form-bean> specifies the dynaactionform class in the Struts framework package. You can also extend this class to implement custom dynamic actionform.
Access dynamic actionform in action
Like normal actionform, the action class and JSP can access dynamic actionform, And the access method is basically the same. The biggest difference between accessing a dynamic actionform and accessing a common actionform is that the access to attributes is different. In the standard actionform, The getter and setter methods are provided for each attribute to read and set attributes, while dynaactionform saves all attributes in a map object, therefore, accessing the attributes in dyanactionform is similar to accessing the map object:
Here, I use the dynamic actionform configured above to illustrate how to access the dynamic actionform.Dynaactionform DYNAFORM = (Dynaactionform) form;
String Username = (String) DYNAFORM. Get ( " Username " );
String userpwd = (String) DYNAFORM. getstring ( " Userpwd " );
// DYNAFORM. Set ("username", "fengyan"); set the value!
Form Verification of dynamic actionform
The Validate () method of the dynaactionform class does not provide any default verification behavior. You can define the subclass of the extended dynaactionform class and then override the validate () method. However, it is not an ideal way to program dynamic actionform. The validator verification plug-in of struts should be used for dynamic actionform verification. The validator plug-in allows you to use a specific configuration file to configure verification rules for dynamic actionform, such as checking required input fields and date and time.
About dynaactionform verification and verification plug-in I will write in the next note!