Learning model-View-controller MVC pattern

Source: Internet
Author: User
Tags tomcat server

1. Introduction to MVC

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

MVC begins in the desktop program, M refers to the business model, v refers to the user interface, C is the controller, the purpose of using MVC is to separate m and v implementation code, so that the same program can use different representations. For example, a batch of statistical data can be represented by histogram, pie chart. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously.

Model-View-Controller (MVC) is a software design pattern invented by Xerox PARC in the the 1980s for programming language Smalltalk-80, which has been widely used. It was later recommended as a design model for Oracle's Sun Company's Java EE platform and was welcomed by a growing number of developers using ColdFusion and PHP. Model-View-controller mode is a useful toolbox that has many benefits, but also has some drawbacks.

Model is the part of your application that handles application data logic. Typically, model objects are responsible for accessing data in the database.

View is the part of the application that handles the display of data. Typically views are created from model data.

A controller is a part of an application that handles user interaction. Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

MVC is a framework pattern that makes it mandatory to separate the input, processing, and output of an application. Using an MVC application is divided into three core parts: model, view, controller. Each of them handles their own tasks. The most typical MVC is the JSP + servlet + JavaBean pattern.

To put it simply, the three-tier architecture differs from MVC.

The three-tier architecture divides the entire business application into the interface layer (User Interface layer), the business logic layer, and the data access layer.

MVC and the three-tier architecture are the same place where there is a presentation layer, the interface. And the other two places are different. The concept of a controller is not defined in the three-tier architecture. This is the most different place. and MVC does not consider the logical access of the business as two layers, which is the main difference between a three-tier architecture or an MVC build program. The model is also mentioned in layer three, but the concept of model in the three-tier architecture is not the same as the concept of model in MVC, and the typical model layer in the "three layer" is made up of entity classes, while MVC is composed of business logic and access data.

2. Main components of Java Web applications

(1) JSP components:

JSP is a Dynamic Web page technology. It adds the HTML page to the Java script, and the JSP tag forms the JSP file. When a browser requests a JSP page, Tomcat translates the JSP page into a Java file. It then compiles it into a class file, executes it, and returns the resulting HTML page to the client display.

(2) servlet components:

The servlet runs in the Web container. It can be dynamically loaded by the Web container, receiving browser requests, calling other components to process the request, and returning the processing results. When a browser accesses a servlet, the Web container creates a ServletRequest object and a Servletresponse object, and encapsulates the user's request information in the ServletRequest object. The two objects are then transferred as parameters to the specific method of the servlet. In this method, the request is processed, the processing result is encapsulated in the Servletresponse object, and returned to the Web container. Finally, the Web container returns the results to the browser to parse and display.

(3) JavaBean components:

The JavaBean component is a Java object that conforms to a specific specification. A series of properties are defined in the JavaBean component, and each property provides a setter and getter method so that you can use the component to store some intermediate data. For example, ServletRequest request information, information from the database, and so on.

(4) Web server

The Web server is used to process HTTP requests and return HTTP data. It is mainly used to interact with the browser to provide services to the user. such as the Tomcat server.

3. Java Web Solution (development method)

(1) JSP development

(2) Jsp+javabean development

(3) Jsp+servlet+javabean development

(4) Other development methods.

4. Common Java Web MVC framework

(1) Jsp+servlet+javabean

This is the most basic Java Web-based MVC development Framework, JSP is mainly used for interface display, equivalent to View;javabean used to interact with the database, access to data, equivalent to Model;servlet used to get interface input information, The data obtained by JavaBean and database interaction are obtained and uploaded to JSP for display.

(2) Struts2

By adopting JAVASERVLET/JSP technology, the application framework of MVC design pattern based on Java eeweb application is realized, and it is a classic product in MVC Classic design pattern. The model section uses JSP implementations using the Javabean,view section, and the controller is implemented using the Actionservlet provided by struts.

(3) Spring MVC

Spring MVC is a Java-based lightweight web framework that implements a request-driven type of web MVC design pattern. The model encapsulates the data of the application and the Pojo that are typically made up of them. The view is responsible for rendering the model data and generally the HTML output it generates, which the client's browser can interpret. The controller is responsible for processing the user's request, establishing the appropriate model, and passing it on to the view rendering.

(4) SSH

The internet looked up a lot of information, the following is personal understanding.

Use SSH for Java Web Development, get page data and process the process: jsp→action→service→dao,dao→service→action→jsp. The jsp→action is to obtain the page data, Action→service→dao is the data of the Action to the Service layer for logical processing, logical processing needs to interact with the database, then call DAO for data access. Processing is complete, the result is returned, and the action is transferred to a different JSP page depending on the result. So, the individual feels that SSH covers both MVC and the three-tier architecture. Three-tier architecture: DAO is the data Access layer, service is the business logic layer, and JSP is the presentation layer. Mvc:service and DAO make up Model,action is controller,jsp is view.

Learning model-View-controller MVC pattern

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.