The life cycle of the SPRINGMVC __MVC

Source: Internet
Author: User

The life cycle of SPRINGMVC, no response when heard, SPRINGMVC and life cycle. Now it seems that the life cycle is the SPRINGMVC process, SPRINGMVC is an implementation of the MVC idea, look at the flowchart of the MVC idea below:

The core idea of MVC is as above, so how does Springmvc realize the idea of MVC?

Step one: The user initiates the request to the front-end controller (dispatcherservlet)

Step two: The front-end controller requests the processor mapper (handlermappering) to locate the processor (Handle): Find

through XML configuration or annotations Step three: After finding the processor mapper (handlermappering) like the front-end controller back to the execution chain (handlerexecutionchain)

Step Fourth: The front-end controller (Dispatcherservlet) calls the processor adapter (Handleradapter) to perform the processor (Handler)

Step Fifth: The processor adapter to perform the Handler

Step Sixth: Handler the processor adapter back to Modelandview

step seventh: The processor adapter returns Modelandview to the front-end controller
(Modelandview is an underlying object of the SPRINGMVC framework, including Model and view

eighth: Front-end Controller Request View parser (Viewresolver) to resolve views
based on logical view name to Real view (JSP)

nineth step: View resolver like Front controller return view

Tenth step: Front-end controller renders rendering
view rendering of the model data (in the Modelandview object) to the Request field

11th Step: Front-End controller responds to user results
code Example (1) Add the following code to the Web-inf/web.xml:
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet- Class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param- Name>contextconfiglocation</param-name> <param-value> Classpath*:spring-servlet.xml & 

  lt;/param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.html< ;/url-pattern> </servlet-mapping> 

The above configuration is the front-end controller Dispatcherservlet, in servlet-mapping configured *.html, means that all the. HTML end of the request will pass through this servlet. When Dispatcherservlet starts, the default is to find a spring-servlet.xml profile in the Web-info directory, where we specify the location of the file, that is, the spring-servlet.xml under the classpath. If we do not specify the name of the profile spring will look in the Web application's Web-inf folder for a configuration file named [Servlet-name]-servlet.xml, such as Dispatcher-servlet.xml (2) Add spring-servlet.xml files under the Classpath

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= 
"Http://www.springframework.org/schema/beans" > 
  <context:component-scan base-package= "Com.pango.spring.helloworld.controller"/> 
  <bean id= " Viewresolver "class=" Org.springframework.web.servlet.view.UrlBasedViewResolver "> 
    <property name=" Viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value=" 
    / web-inf/jsp/"/> 
    <property name=" suffix "value=". jsp "/> 
  </bean> 

The above file, we have defined a context tag, so that the label defines later, when spring is started, All of the components under this package and Com.pango.spring.helloworld.controller are loaded (this is the automatic scanning mechanism of the package, that is, spring will be marked with @controller @ Classes such as component are loaded into Spring container management).

After the bean tag is configured, Viewresolver is an attempt parser, the fifth step in the Springmvc lifecycle, and when we return a view from a backend controller, the front-end controller returns a specific view based on this section of configuration. such as back-end control returns a Hello, according to the above configuration, the last front-end controller will group and into such an address:/web-inf/jsp/hello.jsp, and then from the/web-inf/jsp/directory below to find a hello.jsp back to the client. (3) write a Helloworldcontroller class under the package

@Controller public 
class helloworldcontroller{ 
@RequestMapping (value= "/hello") to public 
  String SayHello ( Modelmap modelmap) { 
    modelmap.put ("SayHello", "Hello World"); 
    return "/hello";  
  } 

Controller declares that this class is a controller, and the second part of the above shows that as soon as the @controller is added, spring will incorporate this class into spring container management through an automated scanning mechanism. @RequestMapping (value= "/hello"), this definition is a request path, as long as the/hello path will be given to the controller SayHello method to deal with. Finally we return the/hello view to the client. Summary

A preliminary understanding of the SPRINGMVC life cycle in Java, it is an implementation of the MVC idea, previously heard MVC is a design pattern, but in the classic 23 design patterns do not have the MVC pattern, it is actually the Observer Mode (Observer) (pub/sub), A consolidation of the policy model (strategy) and the combination mode (composite), which can be further studied in detail. Springmvc life cycle (another article)

A the client sends an HTTP request, as long as the form of the request conforms to the *.action configured in the Web.xml file, it is handled by Dispatcherservlet.

B Dispatcherservlet the HTTP request to the object of the mapper to give the HTTP request to the corresponding action to process

The mapper compares <bean name= "/hello.action" according to the customer's HTTP request
If the match is correct, then give the HTTP request to the programmer's action

Executes the business method in action, eventually returning an object called Modelandview, which encapsulates the logical name of the data and views sent to the view

Modelandview object with response to Dispatcherservlet

At this time Dispatcherservlet received the Modelandview object, it does not know what the meaning of the view logic name, but also commissioned a named View parser object to specifically resolve the contents of the Modelandview object to resolve the view parser after the content, Once again to the Dispatcherservlet core controller, the core controller then forwards the request to the specific view page, takes out the data, then displays to the user

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.