A primer on SpringMVC

Configure the core controller, a Servlet

<? xml version="1.0" encoding="UTF-8"? > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <! -- the core of configuration spring MVC controller -- > < servlet > < servlet - name > SpringMVCDispatcherServlet < / servlet - name > < servlet -class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <! -- Configures initialization parameters for readingSpringMVCConfig file --> <init-param> <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:SpringMVC.xml</param-value>
    </init-param> <! - the configurationservletThe object was created when the application was loaded. The value must be a non-0 positive integer, indicating the boot sequence.load-on-startup< / a > 1load-on-startup> 
</servlet>
 <servlet-mapping>
     <servlet-name>SpringMVCDispatcherServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
</servlet-mapping>
</web-app>
Copy the code

Configure the SpringMVC configuration file, springmVC.xml

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <! <context:component-scan base- - configures the package to scan when creating the Spring containerpackage="com.itheima"></context:component-scan> <! -- Configure the view parser --> <bean id="viewResolver" 
   class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
</bean> 
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
Copy the code

Write the controller and use the annotation configuration

@Controller
public class HelloController{
      @RequestMapping("/hello")
      public String hello(a){
       System.out.println(Output "hello");
       return "success"; }}Copy the code

To test it, return a success page and jump to the success page

Introduction to the execution process and principle analysis of the case

Case execution process

Case execution process

1. After Tomcat is started, load the web. XML file, instantiate and initialize the Servlet()

Load the springmVC.xml configuration file, create the Spring container, and initialize the objects in the container according to the configuration

3. The client initiates a request and the request reaches the front-end controller.

4. Assign the front-end Controller to the processor mapper and find the corresponding Controller and method

5, return to the front Controller, the front Controller sends a processor adapter, all the Controller and method are suitable, and execute a method, return ModelAndView to the front Controller

6. The front controller then requests the View resolver and returns the View.

7. View rendering fills the model data into the Response field

SpringMVC request response process

The component involved in the getting started case is DispatherServlet: the front-end controller,

When the user request reaches the front-end controller, it is equivalent to C in MVC mode. DispatcherServlet is the center of the whole process control, and it calls other components to handle the user’s request. The existence of dispatcherServlet reduces the coupling between components.

HanderMapping: Adapter mapper. HandlerMapping is responsible for finding handlers, namely processors, according to user requests. SpringMVC provides different mapper to implement different mapping methods, such as configuration files, implementation interfaces, annotations, and so on.

Handler: The Handler, which is the specific business controller we will write during development. The DispatcherServlet forwards the user request to the Handler. The specific user request is processed by Handler.

HandlAdapter: Processor adapter that executes on the processor through a HandlerAdapter. This is an application of the adapter pattern, which can be extended to execute on more types of processors.

The View Resolver: The View Resolver is responsible for generating the View from the processing result. The View Resolver first parses the logical View name into the physical View name, i.e. the specific page address, and then generates the View object. Finally, it renders the View and presents the processing result to the user through the page.

View: View. The SpringMVC framework provides support for many View types, including jstlView, freemarkerView, pdfView, etc. The most common view we use is a JSP. In general, the model data needs to be presented to the user through the page by page tag or page template technology.

MVC: Annotation-driven shows that among various components of SpringMVC, processor mapper, processor adapter and view parser are called the three components of SpringMVC. Using MVC: annotation – driven automatic loading RequestMappingHandlerMapping mapper (processing) and RequestMappingHandlerAdapter adapter (processing), You can use MVC :annotation-driven instead of the annotation processor and adapter configuration in the SpringMVC.xml configuration file.

RequestMapping annotations

Source:@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Mapping 
public @interface RequestMapping { }
Copy the code

Purpose: To establish a mapping between the request URL and the method used to process the request.

Value: Specifies the REQUESTED URL. It does the same thing as the path property.

@RequestMapping(“/findAccount”)

Method: Specifies the request method.

@RequestMapping(value=”/saveAccount”,method=RequestMethod.POST)

Params: Used to specify conditions that restrict the request parameters. It supports simple expressions. The key and value of the request parameters must be exactly as configured

@RequestMapping(value=”/removeAccount”,params= {“accountName”,”money>100″})