Spring MVC principle and annotation development

Posted by solon on Tue, 08 Mar 2022 09:13:50 +0100

3. Spring MVC implementation principle

The figure shows a relatively complete flow chart of spring MVC. The solid line indicates the technology provided by the spring MVC framework, which does not need to be implemented by developers, and the dotted line indicates that it needs to be implemented by developers.

Briefly analyze the execution process

  1. Dispatcher servlet represents the front controller and is the control center of the whole spring MVC. The user sends a request, and the dispatcher servlet receives the request and intercepts the request.

    Our request url is assumed to be: http://localhost:8080/SpringMVC/hello

    As above, the url is divided into three parts:

    http://localhost:8080 Server domain name

    Spring MVC is a web site deployed on the server

    hello indicates the controller

    Through analysis, the above url is expressed as: request the hello controller of the spring MVC site located on the server localhost:8080.

  2. HandlerMapping is processor mapping. DispatcherServlet calls HandlerMapping, which looks up the Handler according to the request url.

  3. HandlerExecution refers to a specific Handler. Its main function is to find the controller according to the url. The controller found by the url above is: hello.

  4. HandlerExecution passes the parsed information to DispatcherServlet, such as parsing controller mapping.

  5. HandlerAdapter represents the processor adapter, which executes the Handler according to specific rules.

  6. The Handler lets the specific Controller execute.

  7. The Controller returns the specific execution information to the HandlerAdapter, such as ModelAndView.

  8. The HandlerAdapter passes the logical name or model of the view to the dispatcher servlet.

  9. DispatcherServlet calls the view resolver to resolve the logical view name passed by the HandlerAdapter.

  10. The view parser passes the parsed logical view name to the dispatcher servlet.

  11. DispatcherServlet calls the specific view according to the view result parsed by the view parser.

  12. The final view is presented to the user.

4. Annotation development

1. Create a new Moudle, springmvc-03-hello-annotation. Add web support!

2. As Maven may have the problem of resource filtering, we will improve the configuration


3. In POM XML file introduces related dependencies: mainly Spring framework core library, Spring MVC, servlet, JSTL, etc. We have already introduced in parent dependency!

4. Configure web XML core file


/Difference between and / *: < URL pattern > / < / url pattern > will not match jsp, only for the requests we write; Namely: jsp will not enter the DispatcherServlet class of spring< URL pattern > / * < / url pattern > will match * jsp, when returning to the jsp view, it will enter the DispatcherServlet class of spring again, resulting in a 404 error because the corresponding controller cannot be found.

    • Pay attention to the web XML version problem, to the latest version!
    • Register DispatcherServlet
    • The configuration file associated with spring MVC
    • The startup level is 1
    • The mapping path is / [do not use / *, it will 404]

5. Add Spring MVC configuration file

Add springmvc servlet. In the resource directory XML configuration file. The configuration form is basically similar to the Spring container configuration. In order to support annotation based IOC, the function of automatic package scanning is set. The specific configuration information is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <context:component-scan base-package="com.wyz.controller"/>
    <!-- Give Way Spring MVC Do not process static resources -->
<!--    support mvc Annotation driven
       stay spring Generally used in@RequestMapping Annotation to complete the mapping relationship
       To make@RequestMapping Note effective
       Must register with context DefaultAnnotationHandlerMapping
       And one AnnotationMethodHandlerAdapter example
       These two instances are handled at the class level and method level respectively.
       and annotation-driven Configuration helps us automatically complete the injection of the above two instances.-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
  1. In the view parser, we store all views in the / WEB-INF / directory, which can ensure the security of the view, because the files in this directory cannot be accessed directly by the client.

    • Make IOC comments effective
    • Static resource filtering: HTML JS . CSS . Pictures, videos
    • Annotation driven MVC
    • Configure view parser

6. Create Controller

public class HelloController {
    public String helloController(Model model){
        model.addAttribute("msg","Hello SpringMVC Annotation!");

        return "hello";
  • @The Controller is used to automatically scan the Spring IOC container during initialization;
  • @RequestMapping is to map the request path. Here, because there are mappings on classes and methods, the access should be / HelloController/hello;
  • The purpose of declaring Model type parameters in the method is to bring the data in the Action to the view;
  • The result returned by the method is the name of the view hello, and the prefix in the configuration file becomes WEB-INF / JSP / hello jsp.

7. Create view

  1. Create hello.jsp in the WEB-INF/ jsp directory JSP, the view can directly take out and display the information brought back from the Controller;

  2. You can get the value or object stored in the Model through EL representation;

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

8. Run Tomcat test

Topics: Spring MVC