For SpringMVC profile

  1. MVC:model view controller
  2. SpringMVC is an MVC implemented with Spring. It is a part of Spring Framework and a lightweight Web Framework based on Java to implement MVC.
  3. SpringMVC features:
  1. Lightweight and easy to learn
  2. Efficient, response-based MVC framework
  3. Good compatibility with Spring, seamless integration
  4. Convention over Configuration
  5. Powerful functions: RESTful, data validation, formatting, localization, themes, etc
  6. Concise and flexible

Implementation principles of SpringMVC

Among them

  1. The DispatcherServlet represents the front controller and is the control center for the entire SpringMVC. The user sends a request, and the DispatcherServlet receives the request and intercepts it.
  2. HandlerMapping maps processors. DispatcherServlet call HandlerMapping HandlerMapping, according to the request url search Handler
  3. HandlerExecution represents a specific Handler whose primary purpose is to find the controller based on the URL and pass the parsed information to the DispatcherServlet
  4. A HandlerAdapter represents a processor adapter that executes handlers according to specific rules, and handlers let specific controllers execute.
  5. The DispatcherServlet calls the ViewResolver to resolve the logical view name passed by the HandlerAdapter.

SpringMVC getting started applet

  1. Create a Maven project and delete the SRC file as the parent project.
  2. Import dependence
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.10. RELEASE</version>
        </dependency>
    </dependencies>
Copy the code
  1. Create a new submodule and add Web support.
  2. Make sure the submodule has the dependencies it needs to import SpringMVC.
  3. Configure web. XMLNote the configuration here!!

      
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <! -- Register DispatcherServlet -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <! -- Associated with a SpringMVC configuration file -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <! -- Boot level -- 1-->
        <load-on-startup>1</load-on-startup>

    </servlet>

    <!--
       /  匹配所有请求  .jsp除外
       /* 匹配所有请求  包括.jsp
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Copy the code
  1. Configure the core configuration file for Spring, namedspringmvc-servlet.xml

      
<beans xmlns="http://www.springframework.org/schema/beans" 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">

    <! -- Processor mapper -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <! -- Processor adapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <! -- View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Copy the code
  1. Write operation business Controller.java
package controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // Create model and view objects
        ModelAndView mv = new ModelAndView();

        // Encapsulate the object and put the model in mv
        mv.addObject("message"."HelloSpringMVC!");

        // Encapsulate the view and place it in mv
        mv.setViewName("hello");

        / / return the mv
        returnmv; }}Copy the code
  1. inspringmvc-servlet.xmlRegister controller.java beans
    <! Register controller bean -->
    <bean id="/hello" class="controller.HelloController"/>
Copy the code
  1. Write the hello.jsp to jump to in web-INF
<%-- Created by IntelliJ idea. User: Created by Date:2021/3/27
  Time: 16:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${message}
</body>
</html>

Copy the code
  1. Configure Tomcat and run the tests.

At this point, the SpringMVC program is complete, but there may be a 404 error, this is the problem of IDEA itself, you can import the jar package, it seems to have been imported, but it is not, in this case, you need to import the jar package again, as follows :(first open the project structure)

  1. The test results