SpringMVC Tutorial: Core Components, Request Flow, and View Resolution Explained

SpringMVC is a lightweight Java Web MVC framework. Its core capabilities include request dispatching, annotation-driven controller development, and view resolution. It solves common problems in traditional Servlet development, such as scattered routing, high coupling, and cumbersome configuration. Keywords: SpringMVC, DispatcherServlet, RequestMapping.

SpringMVC Technical Specifications at a Glance

Parameter Description
Language Java
Core Protocols HTTP / Servlet
Framework Role Spring MVC framework for the Web layer
Typical Dependencies spring-context, spring-webmvc, servlet-api, jsp-api
Main Entry Point DispatcherServlet
Routing Style Annotation-based mapping, configuration-based mapping
View Technologies JSP, JSTL, and others
GitHub Stars Not provided in the source

SpringMVC Serves as a Control-Layer Framework for Web Request Dispatching

SpringMVC is the Web-layer implementation in the Spring Framework and follows the MVC design pattern. It separates request intake, parameter handling, business dispatching, and view rendering into standard components, reducing the complexity of direct Servlet programming.

Its value is immediate in three areas: first, it uses annotations to define routes; second, it provides a unified request entry point; third, it supports REST-style extensions. Developers only need to write controller methods to map URLs to business logic.

A Minimal Dependency Set Can Boot SpringMVC

<!-- Core Spring container -->
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.0.5.RELEASE</version>
</dependency>

<!-- SpringMVC Web capabilities -->
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>5.0.5.RELEASE</version>
</dependency>

<!-- Servlet API that provides Web runtime interfaces -->
<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>
</dependency>

<!-- JSP API for view rendering -->
<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version>
</dependency>

This dependency set defines the container, Web, and view capabilities required to run SpringMVC.

SpringMVC Quick Start Depends on a Unified Front Controller

The key to starting SpringMVC is not the Controller, but the DispatcherServlet. It acts as the unified entry point for all requests and forwards HTTP requests to the mapper, adapter, and the target handler.


<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

This configuration routes all requests to the DispatcherServlet and loads the SpringMVC configuration file at startup.

Controllers Use Annotations to Bind URLs to Methods

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

@Controller
public class QuickController {

    @RequestMapping("/quick") // Map the /quick request to this method
    public String quickMethod() {
        System.out.println("quickMethod......"); // Print an invocation log
        return "index"; // Return the logical view name
    }
}

This controller demonstrates SpringMVC’s core development model: annotation-based routing + method handling + logical view return values.


<html>
<body>

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

This JSP page verifies whether the view returned by the controller is correctly resolved and rendered.

The SpringMVC Configuration File Scans Controllers and Wires the Runtime Environment

If the controller is not managed by the Spring container, requests cannot reach the handler method. Therefore, at a minimum, spring-mvc.xml must enable component scanning.

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

    <!-- Scan the package that contains controllers so Spring can manage them -->
    <context:component-scan base-package="com.itheima"/>
</beans>

The purpose of this XML is to enable IoC management so that classes annotated with @Controller can be discovered.

SpringMVC Request Processing Relies on Multiple Components Working Together

SpringMVC Request Processing Flow AI Visual Insight: The diagram shows a typical SpringMVC dispatch pipeline: a client request first enters the DispatcherServlet, then HandlerMapping locates the target handler, HandlerAdapter invokes the target Controller, and finally ViewResolver resolves the logical view and returns the page response. The diagram highlights the Front Controller pattern and the framework’s decoupled component design.

When the browser sends a request, the DispatcherServlet takes over first and then looks up a HandlerMapping based on the URL. After it finds the target Handler, the HandlerAdapter invokes the controller method through a unified calling mechanism.

After the controller finishes execution, it returns model data or a logical view name. Then the ViewResolver converts the logical view into a physical path, and the View renders the final output back to the client.

Key Components Form a Low-Coupling Dispatch System

  • DispatcherServlet: The unified entry point responsible for orchestrating the entire request lifecycle.
  • HandlerMapping: Matches a handler based on the request path.
  • HandlerAdapter: Hides invocation differences across different handler types.
  • Handler: The business controller that contains the request handling logic.
  • ViewResolver: Converts a logical view name into the actual page resource.
  • View: Produces the final response content.

This component model is the foundation of SpringMVC’s extensibility and low coupling.

SpringMVC Annotation Mechanics Determine Routing Expressiveness

@RequestMapping creates the mapping relationship between a request URL and a method. You can place it on both classes and methods. At the class level, it usually defines a common prefix. At the method level, it defines the specific resource path.

It also supports attributes such as method and params. The former constrains the HTTP method, while the latter restricts parameter conditions, enabling more precise endpoint matching.

@Controller
@RequestMapping("/user") // Class-level prefix
public class UserController {

    @RequestMapping(value = "/list", params = {"accountName"})
    public String list() {
        return "user-list"; // Return the user list view
    }
}

This example demonstrates how class-level and method-level routes combine, along with parameter-based matching.

SpringMVC View Resolvers Determine How Logical Views Map to Physical Resources

SpringMVC defines default components in DispatcherServlet.properties, including a default ViewResolver. In real projects, developers usually configure InternalResourceViewResolver explicitly to standardize JSP path rules.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

With this configuration, the return value index resolves to /WEB-INF/views/index.jsp. This simplifies controller code and hides the real page path.

FAQ

What is the core difference between SpringMVC and Servlet?

Servlet is lower-level. Developers need to handle routing, parameters, and responses manually. SpringMVC sits on top of Servlet and provides unified dispatching, annotation-based mapping, and view resolution, which significantly improves development efficiency.

Why is DispatcherServlet the core of SpringMVC?

Because it is the Front Controller, and every request passes through it first. It connects mapping, adaptation, execution, and rendering into one processing pipeline, making it the scheduling center of the framework.

Why can a controller return a string to navigate to a page?

Because the returned value is a logical view name. The ViewResolver uses the configured prefix and suffix to convert it into the real JSP path, and then the View completes the rendering.

Core Summary

This article reconstructs SpringMVC fundamentals by covering the core concepts, key components, request processing flow, annotation-based mapping, and XML configuration. It also includes a minimal runnable example to help developers quickly understand how SpringMVC dispatches Web requests.