MVC and struts

Source: Internet
Author: User
Tags ldap
M: Model Mode
V: view
C: Control
(1) It is generally used to describe the three-layer structure of JSP websites. Model is the underlying database, view is the JSP page, and control refers to the control between the two, such as JavaBean and servlet.

MVC originally exists in Desktop Program In, M is the exponential data model, V is the user interface, and C is the controller. The purpose of using MVC is to implement m and V. Code So that the same program can use different forms of representation. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure synchronization between m and V. Once M changes, V should be updated synchronously.

Model-View-controller (MVC) is Xerox PARC in 1980sProgramming LanguageA Software Design Model invented by Smalltalk-80 has been widely used so far. Sun's J2EE platform design model has been recommended in recent years and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.
How MVC works

MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: model, view, and controller. They process their own tasks.

View
A view is the interface on which the user sees and interacts with it. For older Web applications, a view is an interface composed of HTML elements. In New web applications, HTML still plays an important role in the view, however, some new technologies have emerged, including Macromedia Flash and some identification languages such as XHTML, XML/XSL, and WML and Web services.

How to process the application interface becomes increasingly challenging. One major benefit of MVC is that it can process many different views for your applications. In fact, there is no real processing in the view. Whether the data is stored online or an employee list, as a view, it is just a way to output data and allow users to manipulate it.

Model
The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as ejbs and ColdFusion components to process databases. The data returned by the model is neutral, that is, the model has nothing to do with the data format, so that a model can provide data for multiple views. Because the Code applied to the model can be reused by multiple views only once, code duplication is reduced.

Controller
The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on a web page and send an HTML form, the controller does not output anything or process anything. It only receives the request and determines which model component is called to process the request, and then determines which view is used to display the data returned by the model for processing.

Now let's summarize the MVC processing process. First, the Controller receives user requests and decides which model should be called for processing. Then, the model uses the business logic to process user requests and return data, finally, the controller uses the corresponding view to format the data returned by the model and presents it to the user through the presentation layer.

Why use MVC?

Most Web applications are created using procedural languages such as ASP, PHP, and CFML. They mix data-Layer Code such as database query statements and presentation-Layer Code such as HTML. Experienced developers will separate the data from the presentation layer, but this is usually not easy to do. It requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings to us are unquestionable.

First, the most important thing is that multiple views can share a model. As I mentioned, you need to access your application in more and more ways. In this regard, one solution is to use MVC, whether your users want the flash interface or WAP interface, they can be processed using a model. Since you have separated Data from business rules from the presentation layer, You can reuse your code to the maximum extent.

Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be represented in HTML, but they may also be represented in Macromedia Flash and WAP. The model also provides State management and data persistence processing functions. For example, session-based shopping carts and e-commerce processes can also be reused by flash websites or wireless networking applications.

Because the model is self-contained and separated from the Controller and view, it is easy to change the data layer and business rules of your application. If you want to port your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, you only need to change your model. Once you implement the model correctly, the view will display the data correctly regardless of whether the data comes from the database or the LDAP server. Because the three components of an application using MVC are opposite to each other, changing one of them will not affect the other two, you can build a loose coupling component based on this design idea.

For me, the Controller also provides the benefit of using controllers to connect different models and views to meet user needs, in this way, the controller can provide a powerful means to construct an application. Given some reusable models and views, the controller can select a model for processing based on the user's needs, and then select a view to display the processing results to the user.

Disadvantages of MVC
The disadvantage of MVC is that it is not easy to fully understand MVC because it is not clearly defined. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it.

You will have to spend a considerable amount of time thinking about how to apply MVC to your application, and because the model and view must be strictly separated, this also makes it difficult to debug the application. Each component must be thoroughly tested before use. Once your components have been tested, you can reuse them without any scruples.

In my personal experience, since we split an application into three parts, using MVC also means that you are going to manage more files than before, which is obvious. It seems that our workload has increased, but remember that this is not worth mentioning compared to the benefits it can bring us.

MVC is not suitable for small or medium-sized applications. It is usually not worth the candle if you spend a lot of time applying MVC to a small or medium-sized application.

MVC is a good way to create software
The MVC design pattern is a good way to create software. It advocates some principles, such as separation of content and display, which may be better understood. However, if you want to isolate the components of models, views, and controllers, you may need to rethink your applications, especially the application architecture. If you are willing to accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will put your software on a new level in robustness, code reuse, and structure.

(2)
MVC is short for Model-View-controller, that is, Model-View-controller. MVC is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. It has been widely used and is recommended as the design mode of Sun's J2EE platform in recent years, more and more web developers are welcome.
MVC design mode:
MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC divides applications into three core modules: model, view, and controller. They undertake different tasks respectively.
MVC process:
First, the Controller accepts user requests and determines which model should be called for processing. Then, the model processes the corresponding business logic based on user requests and returns data; finally, the Controller calls the corresponding view to format the data returned by the model and presents it to the user through the view.
Advantages and disadvantages of MVC:
1. Advantages of MVC
1) Reliability: The presentation layer and processing layer are completely separated. You can modify the view layer without re-compiling the model and controller.
2) reuse & Adaptability: MVC allows you to use multiple views to access the same model.
3) Rapid Development (Rapid Deployment): Because MVC clearly separates the presentation layer and processing layer, different programmers can develop their own services to increase the development speed.
4) maintainability: Clear layers also make the code clear and the maintainability of the program improved.
2. disadvantages of MVC
The Design and Implementation of MVC is not very easy to understand, but it has high requirements on developers. MVC is just a basic design concept and requires detailed design planning. Strict separation of models and views may make debugging more difficult, but it is easier to find errors. The experiment shows that MVC divides the application into three layers, which means the number of code files increases. Therefore, it is necessary to think about file management.
Struts Overview
Struts is essentially an MVC framework implemented on the basis of JSP model2. In the Struts framework, the model is composed of a JavaBean or EJB that implements the business logic. The controller is implemented by actionservlet and action, and the view is composed of a group of JSP files.
For Web applications using struts framework, when the web application starts, it will load and initialize actionservlet, actionservlet read the configuration information from the struts-config.xml file, store them in various configuration objects, for example, the action ing information is stored in the actionmapping object.
When an actionservlet receives a customer request, the following process is performed.
1) retrieve the actionmapping instance that matches the user request. If the instance does not exist, the system returns invalid information in the user request path.
2) If the actionform instance does not exist, create an actionform object and save the form data submitted by the customer to the actionform object.
3) determine whether form verification is required based on the configuration information. If verification is required, call the validate () method of actionform.
4) if the validate () method of actionform returns NULL or returns an actionerrors object that does not contain actionmessage, the form verification is successful. If the validate () method of actionform returns an actionerrors object containing one or more actionmessages, it indicates that form verification fails, at this time, the actionservlet will directly forward the request to the JSP component that contains the user's submitted form. In this case, no action object will be created and the action's execute () method will be called.
5) The actionservlet determines which action the request is forwarded to based on the actioning information contained in the actionmapping instance. If the corresponding action instance does not exist, create the instance first, and then call the action's execute () method.
6) The execute () method of action returns an actionforward object. The actionservlet is forwarding the customer request to the JSP component pointed to by the actionforward object.
7) the JSP component pointed to by the actionforward object generates a dynamic webpage and returns it to the customer. (3)

MVC, M: model. If it is simple, write a JavaBean and V view. Generally, jsp c: control is used and servlet is used.
Struts is a representative framework of MVC. It is a good box at present.

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.