Spring MVC Architecture and request processing controller, springmvc

Source: Internet
Author: User
Tags map data structure

Spring MVC Architecture and request processing controller, springmvc
Spring MVC Architecture and request processing controller 1: MVC design mode:

(1) data access interface: DAO Layer

(2) processing business logic layer: Service Layer

(3) data entity: POJO

(4) accept and process previous requests: Service

(5) frontend page display: JSP

 

 

This architecture mode is the MVC design mode, which is a software architecture mode of software engineering. It is mandatory to separate the input, processing, and output of software systems. The software system is divided into three basic parts: Model and View Controller)

Resolution:

View: responsible for formatting data and presenting them to the customer

Controller: receives and forwards requests and returns the response to the customer.

Model: A Model object has the most processing tasks and is the main part of an application. It processes and implements data.

Ii. MVC:

JSP MODEL1: when the business process is relatively simple, you can give the controller function to the view for implementation. This mode is collectively referred to as JSP MODEL1.

JSP MODEL2: equivalent to JSP MODEL1. When the business process is complex, You need to hand over the business process to the Controller for implementation. JSP focuses on the display of the view.

.

MVC Process

(1) first, the view provides an interactive interface between the system and the user, and sends the user to the Controller.

(2) The Controller receives user requests and decides which model to call for processing.

(3) The model processes the business logic according to the user request and returns the processing result.

(4) The Controller calls the corresponding view to format the data returned by the model based on the returned processing results, and presents the data to the customer through the view.

 

 

Advantages and disadvantages of MVC:

Advantages:

(1) multiple views share a model, greatly improving Code availability

(2) The MVC modules are independent of each other and loosely coupled.

(3) controllers improve applications and flexibility and Configuration

(4) beneficial Software Engineering Management

Disadvantages:

(1) complicated principles

(2) added the complexity of system structure and implementation.

(3) inefficient access to model data from views

Iii. flowchart of Spring MVC Framework

 

 

1: Spring MVC Environment Construction

(1) introduce the jar File

 

(2) Configure Servlet in web. xml

 

 

(3) create SpringMVC configuration file (springmvc-servlet.xml) (controller that processes requests)

 

 

(4). Create a Controller

 

 

(5) create a view

 

(6) deployment and running

(7) Change HandlerMapping (processor ing)

 

<Property name = "suffix" value = ". jsp"/>

</Bean>

</Beans>

 

 

IV:

(1 .) first, the user sends a request to the front-end Controller (dispatcherServlet). Based on the request information, the user decides which control page (Controller) to process and delegates the request to him, that is, Part D of the control logic of the service controller

(2) After receiving the request, the page controller processes the service and returns a Modelandview (model data name and logical view name) after processing is complete)

(3) the front-end controller revokes the controller. Then, based on the logical view name, it selects the name of the in-view and passes in the model data to render the view.

(4) the front-end controller recovers the controller again and returns the result to the customer. The whole process has ended.

 

 

 

(1 .) when the client sends an HTTP request, the web application server receives the request. If the request ing path matches the DispatcherService (in the web. if specified in xml, the web Container will send the request to DispatherService for processing (dkz)

(2 .) after receiving the request, DispatherService configures the request according to the request information (including URL, request parameters, http methods, and HandlerMapping (in <serviceName>-servlet. in xml), find the method K used to process the request.

(3 .) after DispatherService finds the Handler of the current request based on HandlerMapping, it encapsulates the handler through HandlerAdater and calls Handler through a unified adapter interface. handlerAdapter can be understood as a person who uses Handler to work. Three methods

 

 

Supports (Object handler) method to determine whether a Handler can be used

Handle Method: Handler is used to work

GetLastModified method to get the Last-Modified of the Resource

(4 .) springMvc completed a lot of work during the time when the request information reached the real call of Handler's processing method, he will convert the request information in a certain way and bind it to the input parameter of the request method. The object of the input parameter will be converted, formatted, and checked, after all these operations are completed, the Handler processing method is called to process the corresponding data logic.

(5) After the processor completes business processing, a ModelAndView object is returned to DispatcherServlet. The ModelAndView object contains the logical view name and model data information.

(6 .) the ModelAndView object contains a "logical view name" instead of a real view object. DispatherService resolves the logical view name to a real view object through ViewResolver, of course, the View that displays data can be in jsp, xml, pdf, json, and other data formats,

(7) after obtaining the final View object, DispatherService uses the model data in the ModelAndView object to render the View.

(8) The final client obtains the Response Message. According to the configuration, it can be a common HTML page, xml, or json format data. Z

 

(1) Clear roles.

(2) flexible function configuration

(3) provides a large number of controller interfaces and implementation classes

(4) Truly unrelated to the View layer implementation (JSP, Velocity, xslt, etc)

(5) international support

(6) interface-Oriented Programming

(7) provides a complete set of web application development processes

V:

1. parameter passing: the simplest method is to directly input parameters in the Controller method.

@ RequestMapping:

(1) map D through the request URL

(2) ing through Request Parameters and request methods

2. parameter transfer (Controller to View)

(1) Common ModelAndView Methods

If the return value of the processing method is ModelAndView, it includes view information and model data information. With this object, Spring mvc can render the view model.

*. Add model data

 

 

*. Set the view

 

2. Model

In addition to the ModelAndView object, we can use the Model object provided by Spring mvc to transmit Model data. K

3. Map

Through the previous learning of Model and ModelAndView objects, we can also use Spring MVC Model as a map data structure, which can be processed using map as an input parameter.

 

4. @ ModelAttribute

If you want to put the data object of the input parameter into the data model, you need to use this annotation before the corresponding input parameter. Here, we only need a simple understanding.

5. @ SessionAttributes

This annotation can store the attributes in the model to httpSession, so that the attributes can be shared among multiple requests. Z

 

Vi. View parser

 

 

2. Spring mvc uses the view parser to parse the view and parse the logical view name returned by the Controller's processing method into a real view object.

 

Related Article

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.