Chapter I. Introduction to WEB MVC

Source: Internet
Author: User

Introduction to Web MVC 1.1, the request-response model in Web development:

In the Web world, the steps are as follows:

1, web browser (such as IE) to initiate a request, such as access to hao123 home

2. The Web server (such as Tomcat) receives the request, processes the request (for example, the user adds it, saves the user), and finally produces a response (typically HTML).

3. After the Web server is processed, the content is returned to the Web client (usually our browser), and the client processes the received content (for example, the Web browser will render the HTML content it receives to show to the customer).

So, in the Web world:

is the Web client initiating the request, and the Web server receives, processes, and generates a response.

The general Web server is unable to proactively notify the Web client of the update content. Although there are some technologies such as server push (such as comet), and now HTML5 WebSocket can implement the Web server to actively notify the Web client.

Here we understand the request/response model for Web development, and then we look at what the standard MVC model is.

1.2. Overview of the standard MVC model

MVC Model: is an architectural pattern, it does not introduce new functionality, it just helps us to make the structure of the development more reasonable, so that the presentation and model separation, Process Control logic, business logic calls and display logic separation. 1-2

Figure 1-2

First, let's look at the concept of the MVC (Model-view-controller) ternary group:

Model (model): A data model that provides the data to be presented and therefore contains data and behaviors that can be thought of as domain models or JavaBean components (containing data and behaviors), but are now generally separated: Value Object (data) and service layer (behavior). That is, the model provides features such as model data query and status update of model data, including data and business.

View (view): responsible for the presentation of the model, generally we see the user interface, what the customer wants to see.

Controller (Controller): receive user requests, delegate to the model for processing (state change), after processing the returned model data back to the view, the view is responsible for the display. In other words, the controller does a dispatcher's job.

From Figure 1-1 We also see that in the standard MVC model can proactively push the data to update the view (The Observer design pattern, register the view on the model, update the view automatically when the model is updated), but in web development the model is unable to proactively push the view (unable to proactively update the user interface), Because in web development is the request-response model.

Then let's look at what MVC looks like in the web, and we call it web MVC to differentiate standard MVC.

1.3. WEB MVC Overview

Model-View-controller concept as with standard MVC concepts, refer to 1.2, and let's look at the Web MVC Standard Architecture, 1-3:

1-3

In Web MVC mode, the model cannot proactively push the data to the view, and if the user wants the view to be updated, it needs to send another request (that is, the request-response model).

The concept is almost there, so let's take a look at the evolution of web-side development, and use code to illustrate how Web MVC is implemented, and why use MVC as a model?

1.4, web-side development and development process

Here we simply describe the core of the process, 1-4

Figure 1-4

1.4.1 , CGI: (Common gateway Interface) Public Gateway Interface, a scripting technology used on the Web server, written in the C or Perl language to receive Web user requests and processes, and finally dynamically generate responses to users, But each request will result in a process, heavyweight.

1.4.2 , Servlet: A Java EE Web Component technology, a Web component executed on the server side, used to receive Web user requests and processes, and finally dynamically generate responses to the user. But each request produces only one thread (and the cable pool), lightweight. And can take advantage of many Java EE Technologies (such as JDBC). The essence is to output the HTML stream inside the Java code. But the expression logic, control logic, business logic call mixed. 1-5

Figure 1-5

1-5, this practice is absolutely undesirable, control logic, performance code, business logic object calls mixed together, the biggest problem is directly in the Java code output HTML, so that front-end developers can not do page style and other design and modification, even if the changes are very troublesome, This practice is therefore undesirable in practical projects.

1.4.3 , JSP:(Java Server Page): A Web Component that executes on the server side, is a template page technology that runs in a standard HTML page embedded in the scripting language (now Java only). The essence is to embed Java code in the HTML code. JSP will eventually be compiled into a servlet, but it is simpler and more convenient than a pure servlet development page. But the performance logic, control logic, business logic call or mixed. 1-6

Figure 1-6

1-6, this is also absolutely undesirable, control logic, performance code, business logic object calls mixed together, but rather than directly in the servlet output HTML better, front-end developers can do simple page style and other design and modification (but if the embedded Java Too many scripts are difficult to modify, so the actual project is undesirable.

The JSP nature or servlet will eventually generate a servlet at run time (such as Tomcat, which will be generated under the Tomcat\work\catalina\web application name \org\apache\jsp), but this makes the HTML simple point, But still control logic, performance code, business logic object calls are mixed together.

1.4.4 , Model1: can be considered as the enhanced version of JSP, can be considered as jsp+javabean1-7

Features: Use the <jsp:useBean> standard action to automatically encapsulate the request parameters as JavaBean components, and you must also use Java scripts to execute the control logic.

Figure 1-7

Here we can see that using the <jsp:useBean> standard action can simplify the acquisition/creation of JavaBean, and encapsulate the request parameters to JavaBean, and then look at the Model1 architecture, 1-8.

Figure 1-8 Model1 Architecture

In the MODEL1 architecture, the JSP is responsible for controlling the invocation of logic, representation logic, business Objects (JavaBean), simply simplifying the GET request parameters and encapsulating request parameters than pure JSPs. The same is not good, in the project should be strictly prohibited (or up to the use of the demo).

1.4.5 , Model2: in the Java EE World, it can be thought of as a Web MVC model

The MODEL2 architecture can actually be thought of as what we call the Web MVC model, except that the controller uses Servlets, the model adopts JavaBean, the view uses the Jsp,1-9

Figure 1-9 Model2 Architecture

The specific code examples are as follows:

As you can see from the Model2 architecture, the view and model are separated, and the control logic and presentation logic are separated.

But we also see serious shortcomings:

1. 1. Controller:

1. 1. 1, the control logic may be more complex, in fact, we can according to the specification, such as request parameter submitflag=toadd, we can actually call the Toadd method directly, to simplify the control logic, and each module needs a controller, resulting in control logic may be very complex;

1. 1. 2, the request parameter to the model package is troublesome, if can give the framework to do this thing, we can get liberated from it;

1. 1. 3. Select the next view, relying heavily on the servlet API, which makes it difficult or impossible to change views;

1. 1. 4, to show the view of the model data to be displayed, using the servlet API, the replacement of the view technology is also to replace, very troublesome.

1.2. Model:

1. 2. 1, this model uses JavaBean, may cause the JavaBean component class is very large, generally now the project all uses three layer structure, but does not adopt the JavaBean.

1.3. View

1. 3. 1, is now bound in the JSP, it is difficult to change the view, such as velocity, freemarker, such as I want to support Excel, PDF view and so on.

1.4.5 , service to work Front Controller + Application Controller + Page Controller + Context

That is, the front Controller + Application Controller + Page controller (also known as the action) + context, is also web MVC, but the responsibility is more clear, please refer to the "core Java EE design mode" and "Enterprise Application Architecture Model" 1-10:

Figure 1-10

The running process is as follows:

Duty:

Front Controller : Front Controller, responsible for providing a unified access point for the presentation layer, thus avoiding repetitive control logic in the MODEL2 (unified callback by the front-end controller corresponding function method, such as the front of the submitflag=login to transfer the login method) , and you can provide common logic for multiple requests (such as preparation contexts, and so on), separating the specific view and the specific functionality processing (such as login enclosing the request parameter into the model and invoking the business logic object).

Application Controller : application controller, front controller separation Select specific view and specific function processing, need someone to manage, application controller is used to choose specific view technology (view management) and specific function processing (Page Controller/Command object/action management), a strategy design pattern application, It is easy to switch views/page controllers without affecting each other.

Page Controller (Command) : page Controller/action/Processor: function processing code, collecting parameters, encapsulating parameters to the model, transferring the business object processing model, returning the logical view name to the front controller (and the specific view technology decoupling), delegated by the front controller to the application controller to select a specific view to show, Can be an implementation of the command design pattern. The page controller is also known as a processor or action.

Context : context, remember to prepare the model data for the view in Model2, we put it directly in request (Servlet API-related), and with context, we can place the relevant data in context, which is irrelevant to the protocol (such as Servlet API) Access/Setup model data, typically implemented through the Threadlocal model.

Here, we review the evolution of the entire Web development architecture, and perhaps different web-layer frameworks differ in detail, but with the same purpose:

A clean Web presentation layer:

Separation of models and views;

The control logic in the controller is separated from the function processing (collecting and encapsulating parameters to the model object, calling the business object);

The view selection in the controller is separated from the specific view technology.

A thin Web presentation layer:

The less you do, the better, the thin, the irrelevant code should not be included;

Only responsible for collecting and organizing parameters to model objects, initiating the invocation of business objects;

The controller only returns the logical view name and the corresponding application controller to select the specific view strategy used;

Minimize the use of framework-specific APIs to ensure easy testing.

Here we understand the evolution of web MVC, and let's look at what Spring MVC is, architecture, and how to use it in a HelloWorld.

Introduction to Web MVC in the first chapter

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.