Struts2 's MVC pattern

Source: Internet
Author: User

MVC is an architectural pattern that does not introduce new functionality in itself, but simply directs us to improve the architecture of the application, separating the model and view of the application, and thus resulting in better development and maintenance efficiency.

In the MVC pattern, the application is divided into three parts of the model, view, and controller. The model part contains the business logic and business data of the application, the view part encapsulates the output form of the application, that is, the commonly referred page or interface, while the controller part coordinates the model and view, chooses which model to call to handle the business according to the user request. And ultimately which view is the answer for the user.

The three parts of the MVC pattern are very clear and separate, so each part can be independently changed without affecting the rest, thus greatly improving the flexibility and reusability of the application.

1.1.2 MVC Pattern Basics

In the MVC pattern, an application is divided into three parts of the model, view, and controller, followed by the three parts.

1: First look at the role of the model, view, controller parts

    • Model: is responsible for encapsulating the status of the application and implementing the functionality of the application. Usually divided into data model and business logic model, the data model is used to store business data, such as order information, user information, and business logic model contains the business operations of the application, such as the addition or modification of orders.
    • View: Used to present the contents of the model to the user, who can request the model to be updated through a view. The view obtains the data to be displayed from the model, and then presents it to the user in its own way, which is equivalent to providing the interface to interact with the user, and when the user is working on the interface or completing the completion, it makes a request to the controller by clicking the Submit button or other triggering event.
    • Controller: Used to control the process of an application and to process requests made by the view. When the controller receives the user's request, the user's data is matched with the update of the model, that is, the function of invoking the model to implement the user's request, then the controller chooses the view for the response and presents the updated data to the user.

Struts2 is a web-layer framework designed according to the MVC pattern, mainly dealing with receiving and returning data, while the logical layer and data access are not the framework's Tor. This framework is a servlet in the MVC pattern that is used for processing and forwarding. Web. XML is used to configure the Struts.xml to configure the Struts framework.

The component diagram of MVC describes the interactions between the three parts of the model, view, and controller, which are described in detail in the following order of interaction:

(1) The first is to display the view to the user, the user to operate on this view, and fill in some business data

(2) Then the user clicks the Submit button to make a request

(3) The user request from the view arrives at the controller (servlet), which contains the business functions and related data to be completed in the request.

(4) The controller will process the user request, the data in the request will be encapsulated (a series of Interceptor encapsulation: Modeldriven,params parameter encapsulation), then select and invoke the appropriate model, request the model for status updates, and then select the next view to show to the user.

(5) The model will handle the business functions requested by the user, while maintaining and updating the model State.

(6) When the model state changes, the model notifies the corresponding view, telling the view that its state has changed.

(7) When the view is notified by the model, it will make a status query to the model, get the data that needs to be presented, and then display the data according to how the view itself is displayed.

The next step is to wait for the user to start the cycle again.

So, after the user makes a request, what is the basic response order of the Servlet+jsp+javabean model?

When a user makes a request, the request is received by the controller servlet, the servlet transforms the requested data into a data model JavaBean, then invokes the method of the business logic model JavaBean, and puts the results returned by the business logic model in the appropriate place, such as the requested attribute; Finally, according to the return result of the business logic model, the controller chooses the appropriate view (JSP), and the data is presented to the user by the view.

1.1.3 MVC pattern role

In early development, some programmers did not recognize the benefits of the MVC pattern and did not adhere to the MVC pattern when developing. The result is that the program structure is not clear, the function of the various parts of the confusion, in the business function changes, whether it is the business logic changes or display form modification, have to modify a lot of classes, "reaching", resulting in software development and maintenance inefficiencies, errors.



Data validation when the validation is successful the Execute method returns success if the checksum does not successfully return the input value to the configuration file to find the forwarded page
When the data type conversion error (that is, cannot be converted) The STRUTS2 framework defines the data as the initial value of the Java data, the framework generates an error message and puts the error message into the Addfielderror
If you want to replace the default error message of the STRUTS2 framework with the configuration in the Struts.xml file, there should be an error message in the replacement
#在这里面name是你要替换的框架当中的值, value is the values to be replaced
XWORD.DEFAULT.INVALID.FIELDVALUE={0} error here {0} is the value that is appended to the value when the data is wrong. The string that is to be added to the Addfielderror

The substitution of error messages also has global and local
The global configuration file to be in the classes directory under the local configuration file in the action same directory under the global profile name defined by the properties
The local configuration file is the same as the name of the action


In the configuration file, however, each action property can set the method of this form processing, in the method property is the name of the methods. This is done so that an action class can have multiple processing logic

The validation framework for Struts2 is based on XML validation, where "action class name" + "-validation.xml" is created under the same directory as the action class and then a DTD file is added to the file, which can be copied to the struts2 example.
Then write the configuration file as follows:
<validators >//This is the root directory
<field name= "username" >//This is the name of the input box that needs to be validated.
<field-validator type= "requiredstring" >//Select validation type list of this type in Xwork.jar The default.xml under the com.opensymphony.xwork2.validator.validators bag
In the file
<param name= "Trim" >true</param>//this is configured for the properties in the binding validation class
<message>username should not being blank;</message>//when validation does not pass outdated information output
</field-validator>
</field>
<field name= "username" >
<field-validator type= "Stringlength" >
<param name= "MinLength" >6</param>
<param name= "MaxLength" >10</param>
<message> username should between in ${minlength} and ${maxlength}</message>
</field-validator>
</field>
</validators>

There can be more than one return method in an action class (an action class can implement multiple logic). You can then struts.xml the file to set which execution method is the property setting of the method in the action tag, but using different methods requires a different
Authentication methods, you can set different authentication profiles to set the validation method for different methods, verifying that the file name of the profile should read: Actionname-methodname-validation.xml
This is the way to set the checksum for each method, but the checksum configuration configured for this action class will also be verified, and the checksum of this action class is first checked (it is recommended that if an action class has multiple logic, only the validation of each logic is provided and the action class is not provided
Global validation)
Special case: If an action class has a validation of a method, and there is a global validation, the action has a subclass of this subclass also has a method of validation and global validation, the order of system validation is
First the global validation of the parent class is performed and then the validation of the method is followed by the global validation of the subclass, and finally the method of the child class.


In addition to the field check method (validation for each field) there is also a non-field check validtor that defines a validtor tag in <validators > this tag. The object that defines the checksum then sets the field to be validated for this object
Use of Validtor Tags: the first sub-label under the label is <param name= "FieldName" > The name of the field that needs to be verified </param> then there must be a <message> tags indicate what is prompted after an error





Client-side checksum:
STRUTS2 provides us with a certain client-side check function but the function is not perfect, so it is best to write the code of JS check;
However, the theme property must not be set to simple when using the calibration method provided by Struts2, and the Validata is set to true;
You can also write your own JS check, the form of some properties and HTML is the same

Struts2 's MVC pattern

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.