Mastering Spring MVC: Step-by-Step Flow and Core Code Explained
This article walks through the complete Spring MVC request handling flow, from setting request attributes and locating the handler to executing interceptors, invoking the controller, resolving the view, and finally rendering the response, with detailed code examples for each step.
Spring MVC Process
First, a diagram:
Code location: com.interface21.web.servlet.DispatcherServlet#doService The original Interface21 code is a minimal implementation of Spring MVC; modern Spring has grown much larger, but Interface21 remains an excellent learning reference.
1. Set Attributes
// 1. Set attributes
// Make web application context available
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
// Make locale resolver available
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
// Make theme resolver available
request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);2. Find Handler and Execution Chain
// 2. Find handler and return execution chain
HandlerExecutionChain mappedHandler = getHandler(request);3. Get Handler Adapter
// This will throw an exception if no adapter is found
// 3. Return handler's adapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());HandlerAdapter is intended for framework developers, not application developers; it allows custom web‑workflow implementations.
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object delegate)
throws IOException, ServletException {
// you may need to doSomething...
((MyHandler) delegate).doSomething(request);
return null;
}4. Execute Pre‑handle Interceptors
for (int i = 0; i < mappedHandler.getInterceptors().length; i++) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
// pre interceptor
if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) {
return;
}
}5. Invoke the Real Handler and Obtain ModelAndView
// 5. Execute the real handler and get ModelAndView (handler may be a proxy, possibly with AOP)
ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler());6. Execute Post‑handle Interceptors
for (int i = mappedHandler.getInterceptors().length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
// post interceptor
interceptor.postHandle(request, response, mappedHandler.getHandler());
}7. Resolve View from ModelAndView
View view = null;
if (mv.isReference()) {
// We need to resolve this view name
view = this.viewResolver.resolveViewName(mv.getViewName(), locale);
}8. Render the View
// 8. Render view and return
view.render(mv.getModel(), request, response);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
