Backend Development 10 min read

Understanding Spring MVC: Architecture, Core Components, and Configuration

This article explains the MVC pattern and provides a detailed introduction to Spring MVC, covering its core DispatcherServlet, required web.xml configuration, request processing flow, key components such as HandlerMapping, HandlerAdapter, ViewResolver, and includes code examples and component descriptions for backend developers.

Java Captain
Java Captain
Java Captain
Understanding Spring MVC: Architecture, Core Components, and Configuration

The Model-View-Controller (MVC) pattern separates an application into three interconnected components: Model (data), View (presentation), and Controller (request handling), providing a clear structure for web applications.

Spring MVC is a request-driven framework built around the Servlet API; it routes incoming requests to a front‑controller (DispatcherServlet), which delegates to controller beans, processes business logic, and resolves views for the response.

To use Spring MVC, you must declare the <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> configuration in web.xml , which registers the DispatcherServlet and the ContextLoaderListener.

The request processing flow is: client request → DispatcherServlet → HandlerMapping finds the appropriate Handler (controller) → HandlerAdapter invokes the Handler → Handler returns a ModelAndView → ViewResolver resolves the logical view name → DispatcherServlet renders the view and returns the response to the client.

Key Spring MVC components include:

Front‑controller DispatcherServlet (framework‑provided)

HandlerMapping : maps URLs to controller beans

HandlerAdapter : adapts various controller types for execution

HandlerExceptionResolver : processes exceptions thrown by controllers

ViewResolver : translates logical view names to actual view implementations

View : the final rendering component (e.g., JSP, FreeMarker)

The DispatcherServlet class extends FrameworkServlet and defines numerous constants and properties for multipart handling, locale resolution, theme resolution, and strategy beans. Its core methods onRefresh and initStrategies initialize these components.

package org.springframework.web.servlet;

@SuppressWarnings("serial")
public class DispatcherServlet extends FrameworkServlet {
    public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
    // ... other constant definitions ...
    private boolean detectAllHandlerMappings = true;
    private boolean detectAllHandlerAdapters = true;
    // ... more fields ...
    public DispatcherServlet() { super(); }
    public DispatcherServlet(WebApplicationContext wac) { super(wac); }
    @Override
    protected void onRefresh(ApplicationContext context) { initStrategies(context); }
    protected void initStrategies(ApplicationContext context) {
        initMultipartResolver(context);
        initLocaleResolver(context);
        initThemeResolver(context);
        initHandlerMappings(context);
        initHandlerAdapters(context);
        initHandlerExceptionResolvers(context);
        initRequestToViewNameTranslator(context);
        initViewResolvers(context);
        initFlashMapManager(context);
    }
}

Within each DispatcherServlet instance there is a dedicated WebApplicationContext that holds all framework beans shared between the servlet and the application, such as HandlerMapping, HandlerAdapter, ViewResolver, ThemeResolver, MultipartResolver, and FlashMapManager.

Specific implementations of these interfaces include:

HandlerMapping : SimpleUrlHandlerMapping (XML‑based) and DefaultAnnotationHandlerMapping (annotation‑based)

HandlerAdapter : AnnotationMethodHandlerAdapter for annotated controller methods

HandlerExceptionResolver : SimpleMappingExceptionResolver (XML) and AnnotationMethodHandlerExceptionResolver (annotation)

ViewResolver : UrlBasedViewResolver and its subclasses for JSP, FreeMarker, etc.

These components together enable developers to build robust, modular Java web applications using the Spring MVC framework.

JavaMVCbackend developmentSpring MVCDispatcherServlet
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.