DEV Community

Lunici
Lunici

Posted on

SpringMVC Quickstart

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

<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>
Enter fullscreen mode Exit fullscreen mode

Create a Spring configuration java class

@Configuration
@ComponentScan("com.hang.controller") // Here we only scan the controller directory
public class SpringMvcConfig {}
Enter fullscreen mode Exit fullscreen mode

Create a controller java class

@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
      }
}
Enter fullscreen mode Exit fullscreen mode

Create a Servlet container startup configuration java class

// 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;
      }
}
Enter fullscreen mode Exit fullscreen mode

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.

// 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[]{"/"};
      }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)