Import dependence

<dependencies>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
   </dependency>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>5.1.9. RELEASE</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>jsp-api</artifactId>
       <version>2.2</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
   </dependency>
</dependencies>
Copy the code

Configure web. XML


      
<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">
    <! - 1. Registered DispatcherServlet -- -- >
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <! -- Associated with a springMVC configuration file: [servlet-name] -- servlet.xml-->
        <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>

    <! --/ Match all requests; (not including.jsp) -->
    <! --/* Match all requests; (including.jsp) -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Copy the code

Write for springmvc – 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">

</beans>
Copy the code

Add a process mapper

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
Copy the code

Adding processor adapters

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
Copy the code

Add a view resolver

<! -- View parser :DispatcherServlet to its ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
    <! -- prefix -- -- >
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <! - the suffix - >
    <property name="suffix" value=".jsp"/>
</bean>
Copy the code

Write operation business Controller

Either implement the Controller interface or add annotations; We need to return a ModelAndView, load the data, and seal the view;

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;
// Note: here we enter the Controller interface first
public class NewController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //ModelAndView
        ModelAndView mv = new ModelAndView();

        // Encapsulate the object and place it in ModelAndView. Model
        mv.addObject("msg"."HelloSpringMVC!");
        // Encapsulate the view to jump to and place it in ModelAndView
        mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
        returnmv; }}Copy the code

Register the beans in SpringMVC-servlet.xml

<! --Handler-->
<! --id needs to be/concatenated to the root directory -->
<bean id="/hello" class="controller.NewController"/>
Copy the code

debug

In 404

  1. Check the console output to see if any JAR packages are missing.

  2. If the jar package exists and cannot be displayed, add lib dependencies in the project release of IDEA!

    Create a lib directory under the web-INF of the project structure component and place all dependent packages in it.

  3. Restart Tomcat! z