[1] spring mvc Study Notes

Source: Internet
Author: User

[1] spring mvc Study Notes

Spring mvc Basics

First, summarize the learning steps of the mvc framework:

1. Establish the environment;

2. How to pass the view value to the controller );

3. How does the Controller pass the value to the view;

4. Exception Handling;

5. Page tags;

6. File Upload;

7. In-depth source code;

Let's get down to the point. Next we will start spring mvc development.

Step 1. Import required packages

Use builConfigurePath to create a spring mvc user Libaray and add the necessary spring mvc package to facilitate future use.

The required packages are as follows. You can download them on the spring official website.

Step 2. Understand the runtime mechanism of spring mvc


First, the request arrives at dispatcherServlet (which can be understood as a controller). The controller will find handlerMapping (located in the configuration file ), then handlerMapping will find that the current url request should be handled by the specific controller (that is, he is responsible for a ing between the url and the controller ); after finding the ing relationship, it is processed by the response Controller (processor). Then, the controller interacts with my service, dao, and model layers to obtain a value, and then returns a view; then, the returned View is determined by viewResolver. The View corresponds to which the View is returned in that form, and a View is returned to it. In this way, a complete spring MVC running process is completed.

Step 3. Configure dispatcherServlet in web. xml

Capture all requests to be handled by dispatcherServlet

 
 
  
  
   
    index.jsp
   
  
    
   
    spring
     
   
    org.springframework.web.servlet.DispatcherServlet
   
  
    
   
    spring
     
   
    /
   
    
 
Part 4: Create an xml file in the WEB-INF to take charge of springmvc Configuration

Must be named spring-servlet.xml (consistent with servlet-name in web. xml );

 
 
  
  
   
   
  
 
HandlerMapping is used to create a bean (name is "/welcome.html") in this file and process the corresponding servlet,

Then the view parser ViewResolver

View ing is done through prefix + view name + suffix, for example, a view is returned in the controller, where it will go to/WEB-INF/jsp/+ "url name" +. search for the jsp file in jsp

Step 5 and Step 6: Create the servlet and jsp View

Com. qzo. web. WelcomeController corresponds to the above class

package com.qzp.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class WelcomeController extends AbstractController{@Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {System.out.println("welcome");return new ModelAndView("welcome");}}
Next is the view layer, that is, the jsp page, built under the WEB-INF/JSP/name must be welcome. jsp (here the welcome is consistent with the ModelAndView)
    welcome!! 

The latest welcome.html file will jump to the welcome. jsp page. The above is the execution process of spring mvc.


I

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.