Eighty-seven, SpringMVC framework overview

SpringMVC is also called Spring web mvc. It is part of the Spring framework and was released after Spring 3.0.

content

Advantages of SpringMVC

The first annotated SpringMVC program

Create a SpringMVC configuration file

MVC components of SpringMVC

SpringMVC execution process (understanding)


Advantages of SpringMVC

1. Based on MVC architecture

  • Based on the MVC architecture, the functional division of labor is clear and decoupled.

2. Easy to understand, quick to use; easy to use

  • You can develop an annotated SpringMVC project. SpringMVC is also lightweight and the jar is small. Does not depend on specific interfaces and classes.

3. As part of the Spring framework, you can use Spring's IoC and Aop 

  • It is convenient to integrate other frameworks such as Strtus, MyBatis, Hiberate, JPA, etc.

4. SpringMVC strengthens the use of annotations, and annotations can be used in controllers, services, and Dao, which is convenient and flexible.

  • Use @Controller to create processor objects, @Service to create business objects, @Autowired or @Resource to inject Service into the controller class, and Dao into the Service class.

The first annotated SpringMVC program

The so-called SpringMVC annotation development means that the registration of the processor in the springmvc container can be completed by annotating classes and methods in the code. Annotated development is the focus.

Project: primary-annotation

Completion function: The user submits a request. After receiving the request, the server-side processor gives a welcome message and displays the message in the response page.

1. Create a new maven web project

 2 、 pom.xml

After creating the web project, add Servlet dependencies, SpringMVC dependencies

rely:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>

Plugin:

<build>
<plugins>
<!-- 编码和编译和JDK版本 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

3. Register the central scheduler

 (1) Full class name

The central dispatcher is a servlet named DispatcherServlet. The fully qualified class name of the central scheduler can be found under the first package org.springframework.web.servlet in the imported Jar file spring-webmvc-5.2.5.RELEASE.jar.

(2)<load-on-startup/>

The function of adding <load-on-startup/> in <servlet/> is to mark whether the servlet instance will be created when the web server (here, Tomcat) starts, that is, whether to call the init that executes the servlet when the web server starts. () method, rather than creating it when it is actually accessed.

Its value must be an integer.

  • When the value is greater than or equal to 0, it means that the container loads and initializes the servlet at startup. The smaller the value, the higher the priority of the servlet and the earlier it is created;
  • When the value is less than 0 or not specified, it means that the servlet will be created only when it is actually used.
  • When the values ​​are the same, the container chooses the creation order by itself.

(3)<url-pattern/>

For <url-pattern/>, it can be written as /, and it is recommended to write it as *.do.

(4) Configuration file location and name

After the registration is complete, it can be published and run directly on the server. At this point, when accessing the browser page, the console will throw a FileNotFoundException exception. That is, by default, a configuration file named Servlet Name-servlet.xml should be found from the WEB-INF directory under the project root. The "servlet name" here refers to the name value of the servlet specified in the <servlet-name/> tag of the registration central dispatcher. The configuration file in this example is named springmvc-servlet.xml.

 In general, configuration files are placed in the classpath, that is, in the resources directory. Therefore, when registering the central scheduler, you also need to set the search path and file name for the SpringMVC configuration file for the central scheduler.

 Open the source code of DispatcherServlet, which inherits from FrameworkServlet, and there is an attribute contextConfigLocation in this class, which is used to set the path and file name of the SpringMVC configuration file. The properties of the initialization parameter come from here.

Create a SpringMVC configuration file

Create the SpringMVC configuration file springmvc.xml in the classpath of the project, that is, the src directory. The file name can be arbitrarily named.

1. Create a processor

You can add corresponding annotations to the class and method.

@Controller: Indicates that the current class is a processor.

@RequestMapping: Indicates that the current method is a handler method. This method should process and respond to the URI specified by the value attribute. The method name of the annotated method can be arbitrary.

 If multiple request paths can match the execution of the handler method, an array can be written in the value attribute of @RequestMapping.

The addObject() method in the ModelAndView class is used to add data to its Model. The bottom layer of Model is a HashMap.

The data in the Model is stored in the request scope. By default, SringMVC uses forwarding to jump to the view. After the request ends, the data in the model is destroyed.

2. Declarative component scanner

Register the component scanner in springmvc.xml

 3. Define the target page

Create a new subdirectory jsp under the webapp directory, and create a new jsp page show.jsp in it.

 4. Modify the registration of the view resolver

In order to avoid the redundancy of the request resource path and extension, the SpringMVC framework introduces the request prefix and suffix in the view resolver InternalResouceViewResolver. In ModelAndView, you only need to give the file name of the page you want to jump to. For the specific file path and file extension, the view parser will automatically complete the splicing.

 Put the show.jsp file in the /WEB-INF/jsp/ path.

5. Modify the processor

 6. Use the SpringMVC framework web request processing order

MVC components of SpringMVC

SpringMVC execution process (understanding)

Simple analysis of the execution process

  1. The browser submits the request to the central dispatcher.
  2. The central scheduler forwards the request directly to the processor mapper.
  3. According to the request, the processor mapper will find the processor that handles the request, encapsulate it as a processor execution chain, and return it to the central scheduler.
  4. The central scheduler finds a processor adapter capable of executing the processor according to the processor in the processor execution chain.
  5. The handler adapter invokes the execution handler.
  6. The processor encapsulates the processing result and the view to be jumped into an object ModelAndView and returns it to the processor adapter.
  7. The processor adapter returns the result directly to the central scheduler.
  8. The central dispatcher calls the view resolver to encapsulate the view name in ModelAndView as a view object.
  9. The view resolver returns the encapsulated view object to the central dispatcher.
  10. The central scheduler calls the view object to render itself, that is, fill in the data to form the response object.
  11. The central scheduler responds to the browser.

Guess you like

Origin blog.csdn.net/m0_54925305/article/details/123830449