JavaWeb Framework: Introduction to Spring MVC

Time:2024-2-14

Spring MVC

JavaWeb Framework: Introduction to Spring MVC

summarize

MVC (Model View Controller), as a design pattern for layered development of applications.

Spring MVCSpring MVC provides a front-end controller DispatcherServlet to dispatch requests , and then through the configuration of the handler mapping , view parsing , etc., to make the MVC pattern development more efficient .

Spring MVC five components:Front-end controller DispatcherServletHandlerMappingProcessor ControllerModelAndViewViewResolver

rationale

  • Front-end controller DispatcherServlet receives client requests and calls the corresponding processor Controller based on the HandlerMapping configuration.
  • The Processor Controller will process the request and encapsulate the result into a ModelAndView object and return it to the front-end controller, DispatcherServlet.
  • Front-end controller DispatcherServlet based on the view parser ViewResolver parsing, get the real view object (JSP, etc.) on the processing results of the presentation of the
    JavaWeb Framework: Introduction to Spring MVC

Simple Example:
First, create a Maven web project
JavaWeb Framework: Introduction to Spring MVC

Then, add the dependency in pom.xml

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.25</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.25</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.25</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
</dependencies>

Next, configure web.xml in the webapp/WEB-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <! -- Configure the front-end controller dispatcherServlet -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <! -- Configure where the springmvc configuration files are located -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <! -- Configure initialization parameters to create servlet objects on startup -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <! -- configure url address -->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <! -- All addresses in the root directory, except for other servlets -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Create the springmvc.xml configuration file under the resources file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="cn.edu.springmvcdemo" />
    
    <! -- Configuring the springmvc view parser -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <! -- Configuration view page -->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

After that, create the views file in the webapp/WEB-INF directory, and then create index.jsp under that file.

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

Finally, the test results

package cn.edu.springmvcdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexDemo {
    @RequestMapping("/index")
    public String index(){
        return "index"; //return the corresponding page file name
    }
}

The results are shown in the figure:
JavaWeb Framework: Introduction to Spring MVC