web architecture and MVC architecture

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

About B/S and C/S:

The management software uses the B/S architecture, and the game uses the C/S architecture because it needs to achieve brilliant effects based on the graphics card. Because the B/S architecture is convenient for program maintenance, upgrade and modification, there is still a lot of room for development of B/S in the future. But note that it does not mean that a browser must have a B/S architecture. For example, a small game on a web page is actually a C/S architecture, but it is downloaded while playing. The B/S architecture and the C/S architecture are the most essential. The difference is that B/S is a light client-heavy server architecture, which puts all logic and page materials on the server, and everything on the browser is downloaded from the server, so, and Either there is a browser or a B/S architecture. It should be said that the B/S architecture meets the light client-heavy server model. For example, the WeChat applet does not have a browser, but it is a B/S architecture.

Web Architecture:

There are three types of web architectures. One is that there are only JSP pages, that is, all logic, business processing and page code are on JSP. This method can be used when doing some simple and unimportant programs. It is fast, but not It is conducive to modification and maintenance; the second is JSP and Servlet, which divide the work. JSP is responsible for page code, and Servlet is responsible for logic, data verification and database operations; the third is JSP, Servlet and Java classes, JSP is responsible for page code, Servlet is responsible for data verification, and java class is responsible for logic and database operations. Because the java class does not involve specific business, this java code can be copied as long as it has the same or similar functions, which can speed up the development efficiency. In addition, the servlet needs to be run on the web server. If one day it is said that JSP is no longer used, then the code of the java class can still be used, just replace the code of the JSP and servlet part, but if the code If it is written on JSP or JSP and Servlet, it will be very difficult to modify, and even the code will be useless, and it must be developed from scratch. For example, we have done a program with JSP, Servlet and java. If we need to connect with the WeChat applet at this time, because the WeChat applet does not need a page, we only need to return a JSON data to it. At this time, our JSP and Servlet will be It is unavailable, but the java class can still be retained, we only need to rewrite the data in the servlet to return the JSON data.

MVC Architecture:

Introduction: M refers to the model, V refers to the view, and C refers to the controller. Note that the MVC architecture is not a three-tier architecture. If it has to be classified, it is at most one layer in the three-tier architecture. The MVC architecture is not only used in the B/S architecture. It first appeared in the C/S architecture. The MVC architecture is just a code-solving architecture. Anyone can use it, whether it is Java or C#, whether it is B/S architecture or C/S architecture... Of course, it is impossible for us to write this framework for development in the future. There are special frameworks to implement this architecture, such as strusts, springMVC, etc.

principle:

MVC is used to solve the problem of complicated mesh code jumps. As shown in the figure below, if we have many jumps between pages, it will be as complicated as the figure below:

image

In this way, if there is a code change on a page, it is very likely that the page associated with it needs to be changed. We have to find out and modify it one by one. It is very troublesome and hard to find, and MVC can help us solve this problem, as follows As shown in the figure, MVC has a transfer station. All pages are connected to this transfer station first, and then to which page to jump to, you can find it directly in the transfer station, which is very convenient and concise. The transfer station here is The controller mentioned above.

image

  1. Two models in MVC:

3.1 model 1: View (V) >>> Controller (C) >>> Model (M) >>> View (V)

As shown below:

image

The view sends a request to the controller, the controller finds the model, and the model responds with the relevant page to the view. For example, for a login page, first send a request to the controller. This controller can be made by servlets or filters. As long as the request can be received, the controller can find the corresponding model, that is, the java class, of course In this process, the java class may perform a series of operations such as connecting to the database, and then the java class returns the corresponding page of success or failure to the view after running.

3.2 model 2: View (V) >>> Controller (C) >>> Model (M) >>> Controller (C) >>> View (V)

As shown below:

image

The view sends a request to the controller, the controller finds the model, the model connects to the controller, tells it what to do, and the controller responds with the relevant page to the view. Or take the login example as an example:

image

The view sends a request to the controller, the controller finds the corresponding model, the model connects to the controller, and tells the controller whether it succeeds or fails. The controller is equivalent to having a small ledger, which records which page the login request successfully jumps to. Which page to jump to on failure, and then according to this response to the relevant page to the view.

4. Two examples of interaction model:

First of all, there must be a configuration file. Take JSON as an example here. The content of the configured JSON file is as follows:

image

The mvc in the file is the name of the array. As can be seen from the above figure, there is only one set of data in the array. If the actionName represents login, load the class in the following className. If the returned string is ok, jump to the ok.jsp page. If the error string is returned, jump to the login.jsp page.

Code for the controller part:

image

image

image

Configured as *.action to summarize all requests, let all requests enter this controller first, and then jump to the page according to the configuration file. First, load the configuration file in the initialization method. The reason why the service method is used is that the controller will receive all requests. These requests may be get requests or post requests, so here is the service method to receive all requests uniformly.

The code of the interface Action:

image

TestLogin code:

image

The reason why an interface Action is applied is to load the TestLogin class in the controller and receive the string returned by it. Of course, it is not necessary to apply for the interface method, but the decompile method can be used.

The code of login.jsp:

image

The code of ok.jsp:

image

Demo of running results:

image

image

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326300517&siteId=291194637