Why use the MVC pattern, what are the advantages of the MVC pattern? Please write out the MVC Lightweight framework name you are familiar with.

Source: Internet
Author: User
Tags classic asp

MVC is often said: models, views, and control controllers-it separates business processing from JSP pages. The previous JSP page is to write all the code in the JSP page, so that the purpose of maintaining the MVC pattern is to achieve the functional division of the web system. The benefits of the MVC pattern: 1. Each of them is in the MVC pattern, and each of the three tiers is in the same position, so if there is a change in the needs of that tier, you only need to change the code in the corresponding layer without affecting the code in the other layers. 2. In favor of the development of the Division of labor in the MVC model, because of the separation of the system by layer, then can better achieve the division of labor in the development. Web designers can develop jsps in the view layer, and developers who are familiar with the business can develop the business layer, while other developers can develop the control layer. 3. Facilitates the reuse of components

Layering is more beneficial for component reuse. such as the control layer can be independent into a reusable component, the view layer can also be made into a common operating interface.

The MVC pattern is the abbreviation for "Model-view-controller", and the Chinese translates to "mode-view-controller". MVC applications are always made up of these three parts. Event causes the controller to change the model or view, or to change both. As long as the controller changes the data or attributes of the models, all dependent view will be updated automatically. Similarly, as long as the controller changes the View,view will retrieve the data from the potential model to refresh itself. The MVC pattern was first proposed by the Smalltalk language research group and applied to user interaction applications. There are many similarities between the Smalltalk language and the Java language, both object-oriented languages, and it is natural for sun to recommend the MVC pattern as an architectural model for developing Web applications in the Petstore (pet store) case application. The MVC pattern is an architectural pattern that requires other modes of collaboration to complete. In the Java EE Pattern directory, the service to worker pattern is typically implemented, and the service to worker mode can be composed of a centralized controller mode, a dispatcher mode, and a page helper mode.    While struts only implements the MVC view and controller two parts, the model part needs to be implemented by the developer themselves, and struts provides an abstract class action that enables developers to apply model to the Struts framework. The MVC pattern is a complex architectural pattern, and its implementation is very complex. However, we have ended up with a number of reliable design patterns, combined with multiple design patterns, making the MVC pattern implementation relatively straightforward. Views can be thought of as a tree, which can obviously be achieved with composite pattern. The relationship between views and models can be embodied by observer pattern. Controller controls the display of views, can be implemented with strategy pattern.    The model is usually a mediator and can be implemented using mediator pattern. Now let's take a look at where the three parts of MVC are located in the Java EE architecture, which helps us understand the MVC pattern implementation. The relationship between MVC and the Java EE architecture is that the view is in Web tier or client tier, usually Jsp/servlet, which is the page display section. The controller is also in the Web Tier, usually implemented with Servlets, which is the logical part of the page display. Model is in middle Tier, typically implemented with server-side JavaBean or EJB, the business logic departmentThe implementation of the Division.   MVC Design Idea MVC English is Model-view-controller, that is, an application of input, processing, output flow according to model, view, Controller, such an application is divided into three layers-model layer, view layer, control layer. The view represents the user interface, which can be generalized as an HTML interface for Web applications, but it can be XHTML, XML, and applets. With the complexity and scale of the application, the processing of the interface becomes challenging. An application may have many different views, the MVC design pattern is limited to the processing of the view's data collection and processing, and the user's request, not including the business process on the view. The processing of business processes is delivered to the model.   For example, an order view only accepts data from the model and is displayed to the user, as well as passing input data and requests from the user interface to the control and model. Model: The processing of business processes/States and the development of business rules. The processing of business processes is a black box for other layers, the model accepts the data requested by the view, and returns the final processing result. The design of the business model can be said to be the main core of MVC. The current popular EJB model is a typical application example, it makes a further division of the model from the angle of application technology implementation, in order to make full use of the existing components, but it cannot be used as the framework of application design model. It simply tells you that you can use some of the technical components as a result of this model, which reduces technical difficulties. For a developer, you can focus on the design of the business model. The MVC design pattern tells us that the application model is extracted according to certain rules, and the level of abstraction is very important, which is also the design basis to judge whether the developers are excellent. Abstract and concrete cannot be too far apart, nor too close. MVC does not provide a design approach to the model, but only tells you that you should organize and manage these models to facilitate the refactoring of the model and improve reusability. We can use object programming to make metaphors, and MVC defines a top-level class that tells its subclasses that you can only do this, but there's no limit to what you can do.   This is important for developers of programming. The business model also has a very important model that is the data model. The data model mainly refers to the data preservation (persistence) of the entity object. For example, save an order to a database and get an order from a database.   We can list this model separately, and all operations on the database are limited to that model. The control (Controller) can be understood as receiving requests from the user, matching the model to the view, and collectively completing the user's request. The function of dividing the control layer is also obvious, it clearly tells you that it is a dispatcher, choose what kind of model, choose what kind of view, what kind of user request can be completed. The control layer does not do any data processing。 For example, when a user clicks on a connection, the control layer accepts the request and does not process the business information, it only passes the user's information to the model, tells the model what to do, and selects the view that meets the requirements to return to the user. Therefore, a model may correspond to multiple views, and one view may correspond to multiple models. The separation of models, views, and controllers allows a model to have multiple display views. If a user changes the model's data through a view's controller, all other views that depend on the data should be reflected in these changes. Therefore, whenever any data changes occur, the controller notifies all views of the change, causing the updates to be displayed. This is actually a model of the change-propagation mechanism. The relationship between model, view, controller and its main functions. Second, the MVC design pattern realization ASP. NET provides a similar environment for implementing this classic design pattern. The developer implements the view by developing the user interface in the ASPX page, the function of the controller is implemented in the logical function code (. cs), and the model usually corresponds to the business part of the application system. The implementation of this design in ASP. A multilayer system, which has obvious advantages over the system of the classic ASP architecture. The user display (view) is separated from the action (Controller), which improves the reusability of the code. Separating the data (model) from the Action (Controller) of its operation allows you to design a system that is independent of the data stored in the background.  As far as the nature of MVC structure is concerned, it is a method to solve the problem of coupling system. 2.1 View view is the representation of the model, which provides the user interface.  With multiple user parts that contain a single display page, a complex web page can show content from multiple data sources, and the Web page staff, artists, can participate in the development and maintenance of these web pages alone. Under ASP. NET, the implementation of the view is simple. Page development can be done directly in the integrated development environment by dragging controls, just as you would develop the Windows interface. Each page is described in the form of a composite view: A page consists of multiple sub-views (user parts); A child view can be a Web custom control that is nested within the simplest HTML control, server control, or multiple controls. The page is defined by the template, the template defines the layout of the page, the label and number of user parts, the user specifies a template, and the platform automatically creates the page based on that information. For static template content, such as site navigation on the page, menu, friendly links, these use the default template content configuration, for dynamic template content (mainly business content), because the user's request is different, can only use late binding, and for the user's different, the user part of the display content filtering.  It enhances reusability and prototypes the layout of the site using a combination of pages that are composed of user parts based on the template configuration. The general process of the View section is as follows: first, the page template defines the layout of the page;The profile defines the specific contents of the view label (the user part), then the page is initialized and loaded by the page layout policy class, each user part is initialized according to its own configuration, the validator is loaded and the parameters are set, and the delegate of the event, etc., after the user commits, passes the check of the presentation layer,  User parts automatically submit data to the business entity that is the model. This part mainly defines the Web page base class pagebase, the page layout policy class pagelayout, completes the page layout, is used to load the user part to the page, the user part base class Usercontrolbase namely the user part frame, is used for the dynamic loading inspection part, and personalization of user parts.  In order to achieve the flexibility of Web applications, the View section also uses a number of configuration files such as: template configuration, page configuration, path configuration, authentication configuration, and so on. 2.2 Controller in order to be able to control and coordinate the processing of each user across multiple requests, the control mechanism should be managed in a centralized manner. Therefore, the controller is introduced in order to achieve the purpose of centralized management.  The application's controller set receives the request from the client (typically a user running the browser), decides what business logic functions to perform, and then delegates responsibility for the next user interface to an appropriate view component. The controller provides a centralized entry point for controlling and processing requests, which is responsible for receiving, intercepting, and processing user requests, and delegating the request to the Distributor class, which determines the view that is presented to the customer based on the current state and the results of the business operation. In this section, we mainly define the Httpreqdispatcher (Distributor Class), the Httpcapture (request-capturing class), the controller (Director Class), and so on, and they cooperate with each other to complete the functions of the controller. The request capture class captures the HTTP request and forwards it to the Controller class. The Controller class is the initial entry point for all requests that are processed in the system. The controller completes some necessary processing to delegate the request to the Distributor class, and the Distributor class Distributor is responsible for the management and navigation of the view, which manages which view will be selected for the user and is provided to the distribution resource control.  In this section, the Distributor, strategy, factory method, adapter and other design patterns are adopted respectively. To enable the request-capture class to automatically capture user requests and process them, ASP. NET provides a low-level request/response API that enables developers to service incoming HTTP requests using the. NET Framework class. To do this, you must author a class that supports the System.Web.IHTTPHandler interface and implement the ProcessRequest () method, that is, the request-grabber class, and add the class in the 
  The Web-based MVC framework is an unprecedented boom in the world of Java EE. A new MVC framework is released on the TTS site almost every two weeks. At present, the better MVC, the veteran has struts, webwork. The emerging MVC framework has spring MVC, Tapestry, JSF, and so on. These are mostly works of the famous team, and there are also some fringe team works, such as Dinamica, Vraptor and so on. These frameworks provide a good level of separation capability.  On the basis of good MVC separation, some ready-made auxiliary class libraries are provided, and productivity is improved. How to choose a good framework to apply in your project will be critical to the efficiency and reusability of your project.  This article introduces two of the most popular and commonly used frameworks. Struts struts is part of the Jakarta Project under the Apache Software Foundation. The main architectural design and developer of the Struts framework is Craig R.mcclanahan. Struts is the undisputed king of the current Java WEB MVC framework. After more than five years of development, struts has grown into a stable, mature framework, and occupies the largest market share in the MVC framework. But some of struts's technical features have lagged behind the emerging MVC framework. With spring MVC, Webwork2 these more sophisticated, more scalable frameworks, struts has been challenged as never before.  But in terms of product development, struts is still the surest choice. Struts has a set of mutually collaborative classes (components), Serlvet, and JSP tag lib. The Web application based on struts architecture basically conforms to the design standard of JSP Model2, which can be said to be a type of change of MVC design pattern. Based on the description of the framework, we can easily understand why struts is a web framwork, not just a combination of some tag libraries. But Struts also contains a wealth of tag libraries and utility classes that work independently of the framework. Struts has its own controller, and incorporates other technologies to implement the model and view layers (views). At the model level, struts can be easily combined with data access techniques, including EJB,JDBC and object Relation Bridge.  In the view layer, struts is able to combine these presentation layer components with JSPs, Velocity templates,xsl, and so on. Struts ' architecture struts FRamework is the embodiment of the MVC pattern, so let's look at the Struts Architecture (Architecture) from the model, view, and control separately. The view is primarily built from a JSP, and struts itself contains a set of extensible custom Tag libraries (TAGLIB) that simplifies the process of creating a user interface. Currently includes: Bean tags,html tags,logic tags,nested tags,template Tags these several taglib. For more information about them, refer to the Struts user manual The model model is primarily a representation of the state of a system (sometimes, business logic operations that change the state of the system are also partitioned into the model). In struts, the state of the system is mainly actiomform bean, in general, these States are non-persistent. If these States need to be converted to persistent data storage, struts itself provides utitle packages that can be conveniently associated with database operations from the controller perspective (Controllers) in the Struts framework, The controller is primarily Actionservlet, but the operation of the business logic is mostly coordinated by the action, actionmapping, Actionforward components (perhaps these components should be partitioned into the business logic in the model). Where action acts as the true implementation of the business logic, while Actionmapping and Actionforward specify the direction in which different business logic or processes are run. For how struts controls and processes customer requests, let's specify the four core components of struts. These components are: Actionservlet.  Action classes,action Mapping (here includes Actionforward), Actionfrom Bean. Spring Spring is actually a concrete implementation of the design ideas elaborated in the expert one-on-one. Development. In one-on-one, Rod Johnson advocates the design of the Java EE Pragmatism and provides a preliminary development framework implementation (INTERFACE21 development package) with the book. Spring is a more comprehensive and concrete embodiment of this idea. On the basis of the INTERFACE21 development package, Rod Johnson has been further transformed and expanded to make it a more open,A clear, comprehensive and efficient development framework. Spring is an open-source framework that was created by Rod Johnson and described in his book, "The Java EE Design Development Programming Guide". It is created to address the complexities of enterprise application development. Spring makes it possible to use basic javabeans to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development.  From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. In simple terms, Spring is a lightweight control reversal and face-oriented container framework. Of course, this description is a little too simple. But it does outline what spring does. In order to better understand spring, let's analyze This description: 1, light weight, from the size and cost of the two aspects of spring is lightweight. The full spring framework can be published in a jar file with a size of more than 1MB. and the processing overhead required by spring is negligible. In addition, spring is non-intrusive: Typically, objects in spring applications do not depend on light weight?? Spring is lightweight both in terms of size and overhead. The full spring framework can be published in a jar file with a size of more than 1MB. and the processing overhead required by spring is negligible.  In addition, spring is non-intrusive: Typically, objects in spring applications do not depend on a particular class of spring. 2, control reversal?? Spring facilitates loose coupling through a technique called inversion of Control (IoC). When IOC is applied, objects passively pass their dependencies instead of creating or locating dependent objects themselves. Can you think of the IOC as opposed to Jndi??   Instead of objects looking for dependencies from a container, the container passes the dependency to it when the object is initialized without waiting for it to be requested. 3. Face the plane?? Spring includes rich support for aspect-oriented programming, allowing for the development of cohesive applications by separating the business logic of the application from the system services such as auditing and thing management. Apply objects only do what they should do?? Complete business logic?? That's all.   They are not responsible (or even conscious) for other system concerns, such as log or object support. 4, container?? Spring contains and manages the configuration and lifecycle of application objects, which in this sense is a container. You can configure how each of your beans is created?? Create a separate instance of your bean based on a configuration prototype, or generate a new instance every time you need it?? And how they relate to one another.   However, spring should not be confused with traditional weights of EJB containers, which are often bulky and cumbersome and difficult to use. Framework: Spring enables simple component configuration andComplex applications can be combined. In spring, application objects are combined declaratively, typically in an XML file.   Spring also provides a number of basic functions (transaction management, persistence framework integration, and so on), leaving the development of application logic to you. All of these features of spring enable you to write cleaner, more manageable, and easier-to-test code.  They also provide the basis for various sub-frameworks in spring.

Why use the MVC pattern, what are the advantages of the MVC pattern? Please write out the MVC Lightweight framework name you are familiar with.

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.