Interview Questions frequently encountered during JAVA engineering lions interviews ------ what is MVC design mode and mvc design mode?
As a java engineering lion, you must have experienced many interviews, but every time you are asked about the MVC design pattern, how do you understand a series of questions about MVC such as MVC.
[Occurrence frequency]
[Key test sites]
- The meaning of MVC
- MVC Structure
[Question analysis]
In java Web development, there are two common development modes, usually Mode 1 and Mode 2. Mode 1 uses JSP + JavaBean technology to separate page display from business logic. JSP is used to display pages, and JavaBean object is used to store data and implement business logic. The client directly sends a request to JSP. JSP responds accordingly and calls the JavaBean object. All data is processed by JavaBean and then JSP is returned. JSP generates the final returned result, the structure of model 1 is as follows:
In Model 1, JSP often embeds the code that controls the request process and some logic code. If the code is extracted, it is assumed by a separate role, that is, the controller, then Model 2 is formed, and model 2 conforms to the MVC design pattern, that isModel-View-Controller(Model -- View -- Controller).
MVC-based Web programs are divided into several logical components, making programming easier. It divides objects into three parts according to different functions. The main purpose is to minimize the coupling of various objects. Split into three parts:Model, View, and Controller).
- Model: represents the data of the application and the rules for processing the data. When the model changes, it notifies the view and provides the view with the ability to query the status of the model.
- View: it is used to organize the content of a model. It obtains data from the model and then presents the data to the customer. This role is often assumed by JSP.
- Controller: receives requests from the client and converts these requests to some behavior. These actions are often implemented in models. After these actions are completed, another view is selected to be presented to the customer.
The following is a brief introduction of the most basic development mode (JSP-Servlet) to build an MVC Architecture Model. Later, those frameworks were added on this basis, instead of the corresponding components to implement the MVC design pattern.
In JSP-Servlet development, the Servlet acts as the controller. It accepts requests and distributes them to appropriate JSP pages for user response based on different request information, at the same time, the Servlet also needs to instantiate a JavaBean object. JSP can use the related labels of the JavaBean (such as <jsp: getProterty>) to access the data of the JavaBean. The structure is shown in:
With Model 2, you can clearly separate the display of pages, the processing of business logic, and the control of processes. JSP is responsible for data display, JavaBean is responsible for processing business logic, and Servlet is responsible for process control. The MVC mode makes Web applications easy to expand and maintain. Because different components have different functions, they can be developed and maintained by different people. For example, the front-end siege lions are responsible for JSP pages, giving full play to their art and design talents, and the back-end siege lions are responsible for implementing business logic.
How does the Struts1 framework reflect the MVC pattern?
Although struts1 is basically replaced by struts2 because of its inaccessibility, many old projects still use struts1, so it is necessary to understand the struts1 framework, after all, it used to ride on the battlefield for several years, and can still be seen in some projects.
The Controller of struts1 is composed of ActionServlet, Action, and struts-config.xml. ActionServlet is the entry of struts. All requests are processed by it, and then it determines the corresponding Action to process the request. Action represents an Action, such as user registration and product purchase. The developer's business logic code will also be added here. The configuration file struts-config.xml is the configuration of the entire struts, including the ActionServlet should forward the request to the Avtion, after the Action is responsible for processing, and the JSP file as a response.
The Model of struts1 is mainly implemented by ActionForm. It is similar to JavaBean and contains several readable and writable attributes for data storage and data verification. Generally, an Action is equipped with an ActionForm.
The View of struts1 is mainly implemented by JSP. The data displayed in JSP can either be from ActionForm or the data stored in the Action Scope (request, session, application. Of course, the labels that come with struts can be used to simplify the process.
After developing a Web application using struts1, the developer's thinking method needs to be changed. Action, ActionForm, and JSP are a whole, and each HTTP request must be completed in collaboration with each other. JSP represents what users can see. ActionForm represents data, and Action represents business logic. It shows the MVC components of struts1 and how they work together.
The above is the core design concept of struts1. Most developers only need to complete file configuration and Action, and focus most of their efforts on the business logic implementation in Action. If there are changes to the business, you only need to modify the action. If there are changes on the display, you only need to modify the JSP. The two are coupled and mutually independent. The following figure illustrates the request processing process and principle of Struts.