Struts journey (iii) Struts form processor ActionForm (static and dynamic)

Source: Internet
Author: User


In the previous article, we used logon as an example to explain the struts configuration and implement the login example using the struts framework. Some terms have moved into my mind.

ActionServlet: Struts controller, responsible for intercepting URLs or distributing them. Model (Model layer) and View (View layer) are used, so it can be seen as an intermediary between the Model and View.

ActionForm: It is used to encapsulate the user's request parameters, and the request parameters are passed through the form field of the JSP page.

Action: A bridge between user requests and business logic. Each Action acts as a proxy of business logic and can call business logic.

It is necessary to raise some questions again.

What are the advantages of using the basic MVC and struts framework?

When we know that the framework is not applicable, the typical controller in MVC is servlet. servlet can obtain the call and steering functions of parameters and logical models. Why does struts encapsulate it? When we request a servlet, we get parameters in the servlet, call the business logic, and turn, and we write the turning page in the servlet, when we want to change to a page, we need to modify the code and re-compile the code.

In addition, the data passed from the form is in the string format. We also need to convert the string to the type we need according to actual needs. If conversion is required in many places, and conversion is required every time you use it. Is there a mechanism to automatically convert the strings in the form to the corresponding type? Do we need to manually convert it?

Based on the above inconvenience, the conversion is not flexible, and the strings in the form need to be converted every time for a series of reasons, struts encapsulates these. The repeated operations are extracted and moved to the configuration file, which is more flexible.

In the above problems, struts encapsulates forms. In the web application development process, developers need to spend a lot of time dealing with forms, sometimes some new problems are submitted through forms, some are modifying data through forms, and the processing of all these forms is very complicated in traditional web development. This article focuses on the form processor ActionForm in struts.

ActionForm

Question proposal

In traditional web application development, complicated form processing brings great difficulties to developers. In traditional development languages, if no form is set, the content of the form entered by the user can be automatically collected, and the developer has to manually extract the value of the form in the program. For example, there is a text input field in the form: To obtain the value of this text input field in the program, you can only use the following method: request. getParameter ("password"); this processing method can be used when the form is relatively small, but when there are many input items in the form, you have to repeat the processing similar to the above.

Problem Solving

In Struts, ActionForm is used to solve this problem. For each user's form, an ActionForm is required. This ActionForm automatically saves the form submitted by the customer in this ActionForm, then, the ActionForm is passed to the Action, so that the user information can be retrieved in the Action, and then the corresponding business logic is processed based on the information.

For example, in Struts, the html Tag of struts is expressed as follows:

In this case, after the form is submitted, the struts framework automatically assigns the input item in the form to the password attribute in ActionForm to save the content in the form in ActionForm, the entire process is automatically completed by struts without the interference of developers. When creating an ActionForm, we must follow the following specifications:

(1) Each ActionForm must inherit the org. apache. struts. action. ActionForm class and provide an ActionForm for each form.

(2) Each attribute in ActionForm must correspond to the input items in the form one by one.

(3) Each attribute of AcitonForm must provide the getter method and setter method. The Struts framework uses these methods to save the value of the form and then obtain the value of the form in the Action.

(4) If the form needs to be verified, the validate method must be provided in ActionForm, which provides the specific validation logic for the form. This method not only achieves data verification but also achieves data buffering. In the validate method, it verifies the validity of the form submitted by the user. When the form verification fails, it will automatically return to the user input page, at this time, all user input values are saved in ActionForm. When a page is returned, the struts framework extracts the data from AcitonForm and outputs the data to the corresponding user input items, ensuring that the user starts to input the form information.


Question proposal

The above is static ActionForm. When we create an AcitonForm for each form, the number of actionforms is too large. A strong aggregation of each ActionForm makes it difficult to maintain and reuse code. How can I avoid creating too many actionforms? Besides, when the attribute names of the submitted form are the same, you do not need to create AcitonForm again (for example, login and registration )?

Problem Solving

Dynamic ActionForm can be used in Struts to solve the above problem. Dynamic ActionForm does not need to create its own ActionForm. When creating its own Action, you need to directly convert the form object passed in the execute method to DynaActionForm.

We need to change form-beans configuration in struts-config.xml:

            
                    
                     
              
      
 


Use the get method in Action to obtain the value in the form.

/*** Test dynamic ActionForm. * @ author summer **/public extends Action {@ Override publicActionForward execute (ActionMapping mapping, ActionForm form, signature, HttpServletResponse response) throwsException {DynaActionFormdaf = (DynaActionForm; // retrieve the value of key in map as name and value as class name. stringusername = (String) daf. get ("username"); Integerage = (Integer) daf. get ("age"); System. out. println ("username" + username); System. out. println ("username" + age); returnmapping. findForward ("success ");}}


The static ActionForm method uses the get/set method, while the dynamic ActionForm method uses the getkey method of map, where the key is the value of the tag name.

Advantages of using dynamic ActionForm: if you do not need to re-compile the form and ActionForm, but you need to change the static ActionForm. java file statically, you must re-compile it. Disadvantage: static return is the corresponding value, and dynamic ActionForm returns an object. We also need to forcibly convert this object.

Next MVC evolution to struts MVC Framework

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.