SpringMVC Quickstart
Published: 2022-11-23 20:30:05

## What is SpringMVC？

**SpringMVC is Model View Controller, it is based on Servlet**



## Quickstart

### Be careful!

- **The version of Tomcat cannot be 10 or higher, because it is not compatible with springMVC, and a 404 page will appear. In this quickstart I used tomcat9**
- **This tutorial has been tested with java8 and java18, both can be used**

### Dependencies

```xml
<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.3.21</version>
</dependency>
```

### Create a Spring configuration java class

```java
@Configuration
@ComponentScan("com.hang.controller") // Here we only scan the controller directory
public class SpringMvcConfig {}
```

### Create a controller java class

```java
@Controller
public class UserController {
      @RequestMapping("/save")
      @ResponseBody // This means that the return value of this method is the response for users
      public String save(){
            System.out.println("UserController.Save...");
            return "{'name','hang'}"; // The return value, the user will see it in navegator
      }
}
```

### Create a Servlet container startup configuration java class

```java
// Tomcat startup configuration, load Spring configuration in it
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
      // Load SpringMVC container configuration
      @Override
      protected WebApplicationContext createServletApplicationContext() {
            AnnotationConfigWebApplicationContext webAppCtx = new AnnotationConfigWebApplicationContext();
            webAppCtx.register(SpringMvcConfig.class); // Load Spring configuration
            return webAppCtx;
      }

      // Used to set which requests belong to springMVC processing
      @Override
      protected String[] getServletMappings() {
            return new String[]{"/"}; // means all requests
      }

      // Load the Spring container configuration, you don't need to worry about it here
      @Override
      protected WebApplicationContext createRootApplicationContext() {
            return null;
      }
}
```


## Servlet container startup configuration simplified way

**This way of writing inherits the AbstractAnnotationConfigDispatcherServletInitializer class, which is actually the same as the previous way of writing, except that the repeated part is written by spring. You can use this way during development, which is faster and easier.**

```java
// Tomcat startup configuration, load Spring configuration in it
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
      // Load the Spring configuration file
      @Override
      protected Class<?>[] getRootConfigClasses() {
            return new Class[]{SpringConfig.class};
      }
	  // Load the Servlet container configuration file
      @Override
      protected Class<?>[] getServletConfigClasses() {
            return new Class[]{SpringMvcConfig.class};
      }
	  // Set which requests are managed by springMVC
      @Override
      protected String[] getServletMappings() {
            return new String[]{"/"};
      }
}
```