Master Spring MVC: Step-by-Step Execution Flow Explained

This article walks through building a simple Spring MVC application, shows the necessary configuration files and code, and then breaks down the ten-step request‑handling process from DispatcherServlet to the final view rendered for the user.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Master Spring MVC: Step-by-Step Execution Flow Explained

Have you ever been asked “Talk about Spring MVC execution principle”? Spring MVC’s execution flow is a frequent interview topic, so here is a concise walkthrough.

1. Walk through a simple Spring MVC program

1. In an SSM project, Spring MVC serves as the controller layer to handle requests and return pages, so a controller layer is required.

The HelloController class must implement the Controller interface and override handleRequest.

Code:

public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        String msg = "HelloSpringmvc!";
        mv.addObject("msg", msg);
        mv.setViewName("test");
        return mv;
    }
}

2. Create the Spring MVC configuration file under the resources directory.

Content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- Configure handler mapping -->
    <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- Configure handler adapter -->
    <bean id="controllerHandlerAdapter" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- Configure view resolver -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- Map URL to controller -->
    <bean id="/hello" class="com.zhang.controller.HelloController"/>
</beans>

3. Configure DispatcherServlet in web.xml.

Content:

<?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">
    <!-- Front controller -->
    <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:springmvc.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>
</web-app>

4. Create the JSP view file (see screenshot).

5. Deploy the project to Tomcat and run. If a 404 error appears, verify that the generated WAR contains the WEB-INF/lib directory.

Now the application runs successfully.

2. Understand the execution principle based on the program

The process can be broken down into the following steps: DispatcherServlet: Front controller that receives and intercepts user requests. HandlerMapping: Finds the appropriate handler based on the request URL. HandlerExecution: Passes the resolved URL to the DispatcherServlet. HandlerAdapter: Executes the corresponding controller.

Controller calls the service layer, puts data into a ModelAndView and sets view information. HandlerAdapter returns the view name to DispatcherServlet. DispatcherServlet invokes the view resolver to resolve the view name.

View resolver returns the concrete view to DispatcherServlet. DispatcherServlet forwards to the specific view.

User receives the rendered view.

Execution flow diagram:

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

WebMVCTutorialSpringMVC
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

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.