Struts1.3.8-dynamic action

Source: Internet
Author: User

Using actionform can greatly help developers process HTML forms. It simplifies code writing 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 (dynaactionform) has been introduced. Dynamic actionform removes the need to create a specific actionform class for each HTML form of the application. 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!
Configure dynamic actionform
Dynamic actionform supports complete actionform configuration in the struts configuration file, without any additional Java code. The following is an example:

<! -- Dynamic form demo -->
<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 an ordinary 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.

 

I think dynaactionform has the following Disadvantages:

But the struts-config.xml file has a lot more content, especially when there are many form fields.

When you forget the type and meaning of a field, it is not convenient to check. Struts-config.xml after all is not convenient to read repeatedly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.