[SPRINGMVC] SPRINGMVC study Note One: SPRINGMVC principle and example analysis.

Source: Internet
Author: User

Preface:
today to review the development of the principle of SPRINGMVC, using an illustrated way to analyze the inside, I believe understand the operating mechanism, for the interview Springmvc everyone can say so easy.

One, the graphic method



The front controller of the second graph should be dispatcherservlet. After reviewing the overall process in the diagram, we then use the actual code to illustrate what needs to be done with each step.

Two. Code examples

The first step: the user sends the request, the front controller dispatcherservlet begins processing.
Configure the front-end controller in Web. XML:

1 <servlet>2       <Servlet-name>Springmvc</Servlet-name>3       <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>4       <!--The springmvc configuration file is loaded by default: But the SPRINGMVC configuration file must conform to the specification5 name specification: Servelt-name-servlet.xml======springmvc-servlet.xml6 path Specification: Springmvc-servlet.xml must be under Web-inf7 However, you can specify the name and path of the SPRINGMVC configuration file by configuring the Init-param property.8        -9       <Init-param>Ten           <Param-name>Contextconfiglocation</Param-name> One           <Param-value>Classpath:springmvc.xml</Param-value> A       </Init-param> -   </servlet> -    the   <servlet-mapping> -       <Servlet-name>Springmvc</Servlet-name> -       <Url-pattern>*.do</Url-pattern> -   </servlet-mapping>

Step two: Request for action (Processing mapper processing)
There are three types of processing mapper, three of which are slightly different in configuration and code implementations, with the configuration of the Springmvc.xml in three different modes:
Beannameurlhandlermapping:
Function: The default mapper, complex to find Controller. match Daocontroller According to the name property of the Bean(Controller). Needs to be defined in SPRINGMVC:

Simpleurlhandlermapping: Simple processing Mapper, function: Find controller, the URL is centralized processing.

The specific controller is found by the value area in the prop, when the input abc.do or wangmeng.do or barrywang.do in the browser is mapped to the controller to be executed.

Controllerclassnameurlhandlermapping: Class name mapper: Accessed by class name, lowercase is required for the first letter. Class name if there are more than three words: except for the last one, the initials of the other words are lowercase.
If configured, you can access the controller's name directly on the browser. Use Usercontroller directly in the browser to access it.

Step three: The processor mapper returns chain to the front controller.

Fourth step: Request to execute action.

Fifth step: The processor adapter Handleradapter executes the action.

There are two types of processor adapters.
Simplecontrollerhandleradapter
Function: Execute Controller. Call the Controller method. The default processing makes the configuration

If you use a simple processor adapter, then our custom handler method needs to implement the Controller interface.
Custom handler:

1  Public classUsercontrollerImplementscontroller{2 3 @Override4      PublicModelandview HandleRequest (httpservletrequest arg0, HttpServletResponse arg1)throwsException {5         //accept requests, encapsulate parameters, validate parameters, invoke business methods, return execution results6         //returns the view. 7Modelandview mv =NewModelandview ();8         //set data, any Pojo, in page Echo9Mv.addobject ("Hello", "Sister Feng likes you, very much like you!") ");Ten         //Specifies the return page, specifying the physical view One         //mv.setviewname ("/web-inf/jsps/index.jsp"); A         //Specifies the return page, specifying the logical view -Mv.setviewname ("Index"); -         returnMV; the     } -}

Configuration in SPRINGMVC:

1 <!--Configuring the Processor adapter2 function: Execute Controler, invoke controller method.3 Simplecontrollerhandleradapter: The default processing makes the configuration4  -5 <Beanclass= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></Bean>6 7 <!--Configuring Custom Beans -8 <BeanID= "User"name= "/hello.do"class= "Cn.augmentum.controller.UserController"></Bean>
Httprequesthandlerad Apter: executes the Controller that implements the interface Httprequesthandler.
the custom handler needs to implement the Httprequesthandler interface.
1  Public classItemscontrollerImplementshttprequesthandler{2 3      Public voidHandleRequest (httpservletrequest request, httpservletresponse response)4             throwsservletexception, IOException {5         //to set values in the request domain6Request.setattribute ("Hello", "Obama likes Fengjie!") ");7         8         //Using Request Forwarding9Request.getrequestdispatcher ("/web-inf/jsps/index.jsp"). Forward (request, response);Ten          One     } A}
 

Configuration in Spring.xml:

1 <!--  -2<class = " Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter "></Bean  >

Sixth Step: Custom Handler (Controller) return Modleandview
There are two ways of specifying a return page, specifying a physical view. The other is to specify a return page, specifying a logical view. If you specify a logical view, we also need to configure the view resolver in Springmvc.

Seventh Step: The processor adapter returns the Modleandview to the front controller for processing.Eighth nine step: the front controller sends requests like the view resolver, requests resolution Modleandview, and returns the parsed physical view. (No parsing is required if the physical view is defined by itself)
Configuration is required in Springmvc.xml:

Here is the parse Usercontroller.java passed from: index, after parsing to get the physical view:/web-inf/jsps/index.jsp.

Tenth 11: Render the parsed physical view and then show the index.jsp to the user.

12th Step: Response page.

Here SPRINGMVC the entire implementation process is very clear, here strict use of the MVC design ideas, if the interview encountered SPRINGMVC implementation process problems, I believe you no longer have to worry about.

More recommend you have time to see SPRINGMVC source parsing: http://my.oschina.net/lichhao/blog?catalog=285356

[SPRINGMVC] SPRINGMVC study Note One: SPRINGMVC principle and example analysis.

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.