[SpringMVC] SpringMVC learning notes 1: springmvc principles and instance analysis. springmvc learning notes

Source: Internet
Author: User

[SpringMVC] SpringMVC learning notes 1: springmvc principles and instance analysis. springmvc learning notes
Preface:
Today, I will review SpringMVC's development principles and use illustrated text to analyze the internal information. I believe that after understanding the operating mechanism, SpringMVC can be said to be so easy during the interview.

I. Graphic Display Method



The front-end controller in the second figure should be DispatcherServlet. After reading the overall process in the figure, we will use the actual code to describe what needs to be done in each step.

Ii. Sample Code

Step 1: when a user sends a request, the front-end controller DispatcherServlet starts to process the request.
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 <! -- Spring MVC configuration file loaded by default: But springmvc configuration file must comply with the specification 5 name specification: servelt-name-servlet.xml ====== springmvc-servlet.xml 6 Path Specification: the springmvc-servlet.xml must be under WEB-INF 7 but you can specify the name and path of the springmvc configuration file by configuring the init-param attribute. 8 --> 9 <init-param> 10 <param-name> contextConfigLocation </param-name> 11 <param-value> classpath: springmvc. xml </param-value> 12 </init-param> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name> springmvc </servlet-name> 17 <url-pattern> *. do </url-pattern> 18 </servlet-mapping>

Step 2: request for Action (processing er processing)
There are three types of mappers to process. These three are slightly different in configuration and code implementation. Below we provide springmvc. xml configuration in three different modes:
BeanNameUrlhandlerMapping:
Function: uses the default er to find the Controller. DaoController is matched according to the name attribute of bean (Controller). It needs to be defined in springmvc:

SimpleUrlHandlerMapping: simple processing of the er. function: Search for the Controller and process the url in a centralized manner.

Find the specific Controller through the value zone in prop. When abc. do, wangmeng. do, or barrywang. do are entered in the browser, they will be mapped to this Controller for execution.

ControllerClassNameUrlHandlerMapping.
If configured, you can directly access the controller name in the browser. You can directly access the controller using userController in the browser.

 

If annotation development is used, the annotation processor er is written as springmvc. xml.

1 <! -- Configure annotation processor --> 2 <bean class = "org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerMapping"> </bean>

 

Step 3: The processor er returns the chain to the front-end controller.

Step 4: request to execute Action.

Step 5: processor adapter HandlerAdapterExecute Action.

There are also two processor adapters.
SimpleControllerHandlerAdapter
Function: Execute Controller. Call the Controller method. Default Configuration

If a simple processor adapter is used, the Custom Handler method must implement the Controller Interface.
Custom Handler:

1 public class UserController implements Controller {2 3 @ Override 4 public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {5 // accept requests, encapsulate parameters, verification parameters, call the business method and return execution result 6 // return view. 7 ModelAndView mv = new ModelAndView (); 8 // set the data, any pojo, and display 9 mv. addObject ("Hello", "Feng Jie likes you very much, so you like it very much! "); 10 // specify the return page and the physical view 11 // mv. setViewName ("/WEB-INF/jsps/index. jsp "); 12 // specify the returned page and the logical view 13 mv. setViewName ("index"); 14 return mv; 15} 16}

 

Configuration in springmvc:

1 <! -- Configure the processor adapter 2 function: Execute the Controler and call the Controller method. 3 SimpleControllerHandlerAdapter: Configure 4 by default --> 5 <bean class = "org. springframework. web. servlet. mvc. simpleControllerHandlerAdapter "> </bean> 6 7 <! -- Configure custom bean --> 8 <bean id = "user" name = "/hello. do" class = "cn. augmentum. controller. UserController"> </bean>
HttpRequestHandlerAd Apter:Execute the Controller that implements the HttpRequestHandler interface.
The Custom Handler must implement the HttpRequestHandler interface.
1 public class ItemsController implements HttpRequestHandler {2 3 public void handleRequest (HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException {5 // set the value of 6 requests in the request domain. setAttribute ("hello", "Obama loves Fengjie! "); 7 8 // forward 9 Request. getRequestDispatcher ("/WEB-INF/jsps/index. jsp "). forward (request, response); 10 11} 12}

Configuration in spring. xml:

1 <! -- Create and execute the HttpRequestHandler interface adapter --> 2 <bean class = "org. springframework. web. servlet. mvc. HttpRequestHandlerAdapter"> </bean>


If you use annotation development, the annotation form processor adapter is written as springmvc. xml:

1 <! -- Configure annotation processor adapter --> 2 <bean class = "org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerAdapter"> </bean>

 

 

Step 6: The Custom Handler (Controller) returns ModleAndView
There are two methods. One is to specify the returned page and the physical view. the other is to specify the return page and the logical view. if the logical view is specified, we also need to configure the view parser in springmvc.

Step 7: The processor adapter returns ModleAndView to the front-end controller for processing. step 8/9: the front-end controller sends a request to the view parser, requests ModleAndView resolution, and returns the resolved physical view. (If you define a physical view, no Parsing is required)
In springmvc. xml, You need to configure:

Here is the parsing of UserController. java passed from: index, after the resolution to obtain the physical view:/WEB-INF/jsps/index. jsp.

Step 10/11: render the resolved physical view and then display index. jsp to the user.

Step 2: Response page.

At this point, SpringMVC's entire execution process is very clear and clear. Here we strictly use the MVC design idea. If springmvc execution process is encountered during the interview, I believe you will no longer have to worry about it.

More recommended if you have time to look at Spring MVC source code parsing: http://my.oschina.net/lichhao/blog? Catalog = 285356

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.