Talk about the interview topics often encountered in the Java Engineering Lion interview------What is MVC design pattern

Source: Internet
Author: User

As a Java engineering lion, you've certainly experienced a lot of interviews, but every time you're almost always asked what is the MVC design pattern, how do you understand MVC's similar series of questions about MVC?

"Frequency of occurrence"

"Key Test Centers"

    • The meaning of MVC
    • The structure of MVC

"Exam Analysis"

In Java Web Development, there are two common development patterns, which typically become pattern 1 and pattern 2. Pattern 1 uses Jsp+javabean technology to separate the page display from the business logic, the JSP to implement the page display, the JavaBean object to save the data and implement the business logic. The client makes a request directly to the JSP, the JSP responds, and invokes the JavaBean object, all the data is processed by JavaBean and then returned to the JSP, and the final return result is generated by the JSP, and the structure diagram of Model 1 is as follows:

        

In model one, JSP tends to embed the code of the control request process and some logic code, if this part of the code extracted, by a separate role to bear, the role is the controller, then the Model 2, Model 2 conforms to the MVC design pattern, namely model-View-controller (model--view--controller).

The MVC architecture of the Web program is divided into logical parts, making programming easier to program development. It divides the object into 3 parts according to the function, the main purpose is to minimize the coupling degree of various objects. The 3 parts that are divided into are: model, view, controller.

    • Model: Represents the data of the application and the rules that process the data. When the model changes, it notifies the view and provides the view with the ability to query the relevant state of the model.
    • View: The content used to organize the model, which takes data from the model and then presents the data to the customer, which is often assumed by the JSP.
    • Controller: Responsible for accepting requests from the client and converting those requests into some kind of behavior. These behaviors are often modeled to be implemented, and when these behaviors are completed, a view is chosen to present to the customer.

The following is a brief introduction to the most basic development model (Jsp-servlet) to build an MVC architecture pattern, and later those frameworks are added on this basis, in place of the corresponding components to implement the MVC design pattern.

(Jsp-servlet) Development, the role of a servlet as a controller that accepts requests, distributing them to the appropriate JSP page as the response of the request, and the servlet needs to instantiate a JavaBean object, The JSP can be used to javabean data by using JavaBean related tags (such as <jsp:getProterty>), as shown in the structure:

Using Model 2, the display of the page, the processing of business logic and the control of the process are clearly separated. The JSP is responsible for the data display, JavaBean is responsible for the business logic processing, the servlet is responsible for the process control. The MVC pattern makes it easy for Web applications to scale and maintain, because the individual components are functionally different and can be developed and maintained by different people. For example, the front-end siege Lion is responsible for JSP pages, give full play to their art and design talent, background siege Lion responsible for the implementation of business logic.

How the STRUTS1 framework embodies the MVC pattern

Although Struts1 now basically because of the use of inconvenient basic has been replaced by STRUTS2, but many of the old projects are still written with struts1, so it is necessary to understand the STRUTS1 framework, after all, it has been battlefields several, and now still in some projects can see its figure.

The controller of the STRUTS1 is composed of actionservlet, action, and Struts-config.xml. Actionservlet is the portal to struts, and all requests are processed by it, and it is then decided by the corresponding action to process the request. Action represents a single action, such as user registration, purchase of goods, etc., the developer's business logic code will be added here. Configuration file Struts-config.xml is the configuration of the entire struts, including that Actionservlet should forward the request to the avtion,action responsible for processing, and that JSP file as a response.

The model of STRUTS1 is mainly implemented by Actionform, which is similar to JavaBean, which contains several readable and writable properties for storing data and data validation. In general, an action is equipped with a actionform.

Struts1 views are primarily implemented by JSPs. The data that the JSP displays can come from actionform, or it can be the data that the action holds in scope (request,session,application). Of course, using struts's own labels can be a great simplification.

After using STRUTS1 to develop a Web application, the developer's way of thinking needs to be changed, the Action, Actionform, and JSP are a whole, and each HTTP request needs to be done by the three of them. The JSP represents what the user can see, actionform represents the data, and the action represents the business logic. The various components of STRUTS1 's MVC are shown, and how they work together.

The above is the core design idea of struts1, most of developers only need to complete the file configuration and action, the majority of the focus on action in the business logic implementation above. If there is a change in the business, only need to modify the action, if the display changes, but also only need to modify the JSP, both of which realize the coupling, non-impact. The following figure describes the process and principle of struts processing requests.

Talk about the interview topics often encountered in the Java Engineering Lion interview------What is MVC design 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.