MVC (Model View Controller) framework, mvccontroller
-
MVC Framework
Synonym MVC generally refers to the MVC framework.
The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure. Introduction MVC was originally stored in a desktop program. M refers to the business model, V refers to the user interface, and C refers to the Controller, the purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different expressions. For example, a batch of statistical data can be represented by a column chart and a pie chart respectively. The purpose of C is to ensure synchronization between M and V. Once M changes, V should be updated synchronously. [1-2] Model-View-controller (MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s and has been widely used. Later, it was recommended to be the design model of Oracle's Sun's Java EE platform, 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. [3] (overview content source: [4]) MVC programming mode MVC is a mode for designing and creating Web applications using MVC (Model View Controller Model-View-Controller: [1]
- Model indicates the application CORE (such as the database record list ).
- View (View) displays data (database records ).
- The Controller processes input (written to database records ).
The MVC mode also provides full control over HTML, CSS, and JavaScript.
Model)Is part of the application's data logic.
Typically, model objects are used to access data in databases.
View)Processing data display in the application.
Generally, a view is created based on model data.
Controller)Is the part of the application that processes user interaction.
Generally, the Controller reads data from the view, controls user input, and sends data to the model. MVC layering helps you manage complex applications, because you can focus on one aspect within a period of time. For example, you can focus on view design without relying on business logic. It also makes application testing easier. MVC layering also simplifies group development. Different developers can develop views, controller logic, and business logic at the same time. The framework content MVC is a framework model 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. The most typical MVC is the JSP + servlet + javabean mode. [5] 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 Adobe Flash and some identification languages and Web services such as XHTML, XML/XSL, and WML. the advantage of MVC is that it can process many different views for 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. [6] model models represent 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, such 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. [6] The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on the Web page and send an HTML form, the controller itself does not output anything or perform any processing. 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 returned data. [6] differential editing
Differences between frameworks and Design PatternsMany programmers often confuse the framework and design patterns and think that MVC is a design pattern. In fact, they are completely different concepts. [7] the concepts of frameworks and design patterns are always confusing. In fact, there are still differences between them. The framework is usually code reuse, while the design pattern is design reuse. The architecture is between the two. Some code reuse, some design reuse, and sometimes analysis can also be reused. There are three levels of reuse in software production: Internal reuse, that is, abstract blocks that can be commonly used in the same application; code reuse, which combines common modules into libraries or tool sets, it can be used in multiple applications and fields. the reuse of application frameworks provides general or ready-made infrastructure for specialized fields to achieve the highest level of reusability. Although the framework is similar to the design model, it is fundamentally different. The design pattern is a description of the problem that occurs repeatedly in a certain environment and the solution to the problem. It is more abstract than the framework. The framework can be expressed in code and can be directly executed or reused, for patterns, only instances can be represented by code. design patterns are smaller elements than frameworks. A framework usually contains one or more design patterns, the framework always targets a specific application field, but the same pattern can be applied to various applications. It can be said that the framework is software, while the design mode is software knowledge. What are the framework modes? MVC, MTV, MVP, CBD, ORM, etc. What frameworks are there? C ++ QT, MFC, gtk, Java SSH,
SSIWhat are the design patterns of php's smarty (MVC mode) and python's django (MTV mode? Factory mode, adapter mode, policy mode, etc. In short: the framework is big intelligence, used to divide the software design; the design mode is a small skill, to propose solutions to specific problems, in order to improve the code reuse rate, reduce coupling. StrutsStruts, a common framework Editor, is part of the Jakarta project under the Apache Software Foundation. The main Architecture Design and developer of the Struts framework is Craig R. McClanahan. Struts is an indisputable leader in the Java Web MVC framework. After nine years of development, Struts has gradually grown into a stable and mature framework, and occupies the largest market share of the MVC framework. However, Struts lags behind the emerging MVC framework in some technical features. Struts is facing unprecedented challenges in the face of more sophisticated and scalable frameworks such as Spring MVC and Webwork2. However, from the perspective of product development, Struts is still the safest choice. Struts consists of a group of collaborative classes (components), Servlets, and jsp tag lib. Web applications based on the struts architecture basically comply with the design standards of JSP Model2, and can be said to be a type of change in the MVC design pattern. According to the above descriptions of the framework, it is easy to understand why Struts is a web framework, not just a combination of some tag libraries. However, Struts also contains a wide range of tag libraries and Utility Classes that work independently of the framework. Struts has its own Controller and integrates other technologies to implement Model and View ). In the model layer, Struts can easily be combined with data access technologies, including EJB, JDBC, and Object Relation Bridge. In the view layer, Struts can be combined with JSP, Velocity Templates, XSL, and other presentation layer components. [8] SpringSpring is actually the concrete implementation of the Design idea described in the book "Expert One-on-One J2EE Design and Development. In One-on-One, Rod Johnson advocates the design idea of J2EE pragmatism and provides a preliminary development framework implementation (interface21 Development Kit ). Spring is a more comprehensive and concrete embodiment of this idea. Based on the interface21 Development Kit, Rod Johnson further transformed and expanded it to become a more open, clear, comprehensive, and efficient development framework.
Spring is an open-source framework created by Rod Johnson and described in his book J2EE design and development programming guide. It is created to solve the complexity of enterprise application development. Spring makes it possible to use the basic JavaBeans to accomplish the tasks previously only completed by EJB. However, Spring is not only used for server-side development. From the perspective of simplicity, testability, and loose coupling, any Java application can benefit from Spring.To put it simply, Spring is a lightweight control inversion and cross-section capacity framework. Of course, this description is too simple. But it does summarize what Spring does. [8] ZFZend Framework (ZF) is a fully PHP5-based open-source PHP development Framework supported and developed by Zend. It can be used to develop Web programs and services, ZF uses the Model-View-Controller architecture to separate different parts of the application for program development and maintenance. (For detailed use of the MVC framework and related operations, refer to [8] or refer to articles 2, 3, and 4 for extension .). NET. net mvc [9] is based on the MVC model officially provided by Microsoft. NET Web Application framework, which comes from Castle's MonoRail (Castle's MonoRail is from java). The latest version is. N 4.5. [10] advantages of feature editing
Coupling
LowThe view layer and business layer are separated so that you can modify the view layer code without re-compiling the model and controller code. Similarly, to change the business flow or rules of an application, you only need to change the MVC model layer. Because the model is separated from the Controller and view, it is easy to change the data layer and business rules of the application. The model is self-contained and separated from the Controller and view, so it is easy to change the data layer and business rules of the application. If you port a database from MySQL to Oracle, or change the data source from RDBMS to LDAP, you only need to change the model. Once the model is correctly implemented, the view correctly displays the data from the database or LDAP server. Since the three components of an application using MVC are independent of each other, changing one of them will not affect the other two, a well-coupled component can be constructed based on this design idea. [11]
High reusabilityAs technology advances, more and more methods are needed to access applications. The MVC mode allows different views to access code on the same server, because multiple views share one model, including any WEB (HTTP) browser or wireless browser (wap). For example, you can use a computer or mobile phone to order a certain product. Although the ordering method is different, the ordering method is the same. 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 expressed in HTML, but may also be expressed in WAP. The commands required to represent these data are to change the implementation method of the view layer, the control layer and model layer do not need to be changed. Since data and business rules have been separated from the presentation layer, code can be reused to the maximum extent. 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. [11]
Lifecycle
Low CostMVC reduces the technical content of developing and maintaining user interfaces.
Fast deploymentUsing the MVC pattern, the development time is greatly reduced. It enables programmers (Java developers) to focus on business logic and interface programmers (HTML and JSP developers) focus on the form of expression.
High maintainabilitySeparating the view layer and business logic layer also makes WEB applications easier to maintain and modify.
Favorable Software Engineering ManagementBecause different layers perform their jobs, different applications have some same features, which is conducive to managing program code through engineering and tools. The Controller also provides the advantage 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 applications. 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. [12-13] disadvantages
No clear definitionIt is not easy to fully understand MVC. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it. At the same time, due to the strict separation of models and views, it also brings some difficulties to debug the application. Each component must be thoroughly tested before use.
Not suitable for small and medium-sized applicationsA large amount of time to apply MVC to a large-scale application is usually not worth the candle.
Increase system structure and implementation complexityFor a simple interface, strictly abide by MVC. Separating Models, views, and controllers increases the complexity of the structure and may lead to too many update operations to reduce the running efficiency.
Too close connection between views and controllersViews and controllers are separated from each other, but they are closely related components. A view does not have a controller, and its applications are limited. Vice versa, which hinders their independent reuse.
Inefficient access to model data from a viewDepending on the model operation interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also damage operation performance.
Generally, Advanced Interface tools or constructors do not support modeModifying these tools to adapt to MVC requires a high cost of building separate components, which may cause difficulties in using MVC. [12-13] According to tianji.com, the external evaluation editor shows that the Web-based MVC framework has never been prosperous in the J2EE world, on the TTS website, a new MVC Framework will be released almost every one or two weeks. The better MVC, the old ones are Struts and Webwork. Emerging MVC frameworks include Spring MVC, Tapestry, and JSF. Most of these are works of famous teams, and some other works of edge teams are also outstanding, such as Dinamica and VRaptor. These frameworks provide better hierarchical separation capabilities, based on the implementation of good MVC separation, by providing some ready-made auxiliary class libraries, it also promotes the improvement of production efficiency. [8] selecting a good framework application in a project is crucial to the project's efficiency and reusability. [8] MVC control editing does not have its own control in the ASP. net mvc framework, and the page display is completely back to the time when html code was written. In addition, the asp. net mvc framework also has two built-in help classes: HtmlHelper and UrlHelper. In addition, some help classes are also added in the MvcContrib Extension Project. In this way, we can use not only the complete html to compile the pages to be displayed, but also the help classes, however, html code must be generated at the end of the operation.
HtmlHelper classThe HtmlHelper class is located in the namespace System. Web. MVC. Html. Including FormExtensions, InputExtensions,
LinkExtensions, SelectExtensions, TextAreaExtensions, ValidationExtensions, RenderPartialExtensions, and other seven static classes are all implemented using the extension method. InputExtensions class: There are five types of extension methods used for the CheckBox control, Hidden control, Pass control, RadionButton control, and TextBox Control LinkExtensions class: this class is mainly used to generate related links, the ActionLink and RouteLink methods are mainly extended. ActionLink: the extension method mainly implements a connection. There are 10 overload methods in total.
UrlHelper help classThe class name also knows that this class is used as a URL in the ASP. net mvc application. UrlHelper provides four very common methods. 1. the Action method generates a URL by providing the Controller, Action, and various parameters. the Content method is a virtual, relative path to the absolute path of the application, 3. the Encode method encrypts the URL address and. the Encode method is the same. 4. The RouteUrl method is to provide matching URLs in the routing rules specified in the current application. There are also two attributes: RequestContext and RouteCollection, which respectively contain HTTP context and RouteData. In addition, RouteCollection is the routing rule specified in the current application.
Custom ControlsHtmlHelper provided by Microsoft is enough for most developers, but some features may not meet the requirements of HtmlHelper provided by Microsoft. Next we will talk about how to customize the process. The first custom method is to extend the HtmlHelper object. Three elements of extension method implementation: 1. Static Class 2. Static Method 3. this keyword 1. Define a class first, for example, MyHtmlHelper: using System; using System. collections. generic; using System. linq; using System. web; namespace MvcApplicationFirstDome. models {// static class public static class MyHtmlHelper {// static method} 2. Suppose the method to be extended is GetSpan. When you pass in parameters, the returned result is encapsulated internally. The Code is as follows. Note that the using System. Web. Mvc namespace must be referenced in the MyHtmlHelper class. // Static method public static string GetSpan (this HtmlHelper htmlHelper, string text) {return "<span style = 'color: red '> "+ text +" </span> ";} after the above two steps, the HtmlHelper Extension Method GetSpan is basically ready for use. Next, we will explain how to call it on the page. Pay attention to the next namespace when calling: If the namespace of the extension method is different from the namespace of the page, you must reference the namespace of the extension method, otherwise, there is no way to call custom methods on the page. After referencing the namespace, you can call the custom Extension Method on the corresponding page. For some projects, custom controls are too complex and time-consuming. You can also download the Mvc control that suits your needs from the technical community or source code station. Such as some control packages and table controls. [14]