Detailed description of struts functions-actionform

Source: Internet
Author: User
I. What is actionform?

Actionform can be understood from the following four points:

1. actionform indicates the data of the HTTP form, that is, the page form data collector. It can be seen as an intermediary between the model and view. It is responsible for saving the data in the form for the model or view, only data verified by actionform can be sent to action for processing.

2. actionform is a JavaBean associated with one or more actionconfig. before calling the execute method of the corresponding action, actionform fills itself with the request parameters.

  1. Actionform is an abstract class, which must be inherited when used.
Ii. Functions and usage of actionform
  1. Obtain and save the form parameters. actionform instantiates itself using its own get and set methods. After Initialization is complete, the data in the form is converted to the predefined data type.
  2. Verify form data and actionform verify form data. There are two methods to verify form data: one is to verify the data on the server side, and the other can also be verified on the client side using JavaScript. Verification requires two special classes of actionform, that is, Struts provides us with two useful actionforms. One is dynaactoinform and the other is dynavalidatorform. The following is a brief introduction.
Three: two special actionforms

Dynaactionform class, which is used to reduce the number of actionforms in the system, because actionform is responsible for collecting data in the form. If there is more forms, the number of actionforms increases dramatically, as we can see in Wang Yong's video, there seems to be one or two actionforms, because he has very few functions to demonstrate. Basically, the form is a piece of material information, but this is definitely not the case in a real project, maybe a page may be designed with many forms, and a project has more forms. What should we do? Dynaactionform solves this problem, which makes it unnecessary for us to create a specific actionform class, and uses "org. apache. struts. action. dynaactionform "to create a dynamic actionform. For example, the following login actionform

<form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="actionClass" type="java.lang.String"/> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> </form-beans>

Dynamic actionform is used in the same way as normal actionform, but pay attention to it. The ordinary actionform object needs to provide the getter and setter methods for each attribute. In the preceding example, we need to provide the GetUserName () and setusername () methods to obtain and set the username attribute, there are also a pair of methods for obtaining and setting the password and actionclass attributes.

If dynaactionform is used, it stores the attributes in a hashmap Class Object and provides the corresponding get (name) and set (name) methods. The parameter name is the name of the attribute to be accessed. For example, to access the username value in dynaactionform, you can use similar code:

 

 String username = (String)form.get("username");


Because the values are placed in a hashmap object, dynamic conversion must be performed on the values used. Therefore, not all actionforms are dynamic, we recommend that you use normal actionform for the commonly used actionform.
Dynavalidatorform class

It is a subclass of dynaactionform and can verify the form data. to use it, you must first configure it and then define the plug-in:

 <form-beans> <form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="actionClass" type="java.lang.String"/> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> </form-beans>

Plug-ins

 <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>  </plug-in>

Validator. XML and the validator-rules.xml represent the content of the validation definition and validation rules, which can be combined, respectively ). Verification rules can be defined by ourselves. Use an XML file for definition. The definition rules will not be written here.

Struts checks the form input according to the definition in the XML configuration file and outputs non-conforming error information to the page. However, you may think: although this function is good, what test is executed on the server side. Is there a problem in efficiency and ease of use for users? You may miss the simple JavaScript client verification.

We don't have to worry about it. Struts also supports JavaScript client verification. If you select client verification, Struts starts client verification after a form is submitted. If the browser does not support JavaScript verification, server-side verification is started, this dual verification mechanism can meet the needs of various developers to the maximum extent. Javascript verification code is also defined in the validator-rules.xml file.

The definition process is complicated. I still like to put the verification on the page and let the page developers verify it.

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.