Mastering Spring MVC: Core Concepts, Components, and Annotations Explained
This comprehensive guide explains Spring MVC's MVC architecture, key components like DispatcherServlet and HandlerMapping, request processing flow, transaction management options, and essential annotations such as @Controller, @RequestMapping, @Autowired, and @Transactional, providing Java web developers with a clear, practical overview.
Spring MVC is a lightweight Java web framework that implements the Model‑View‑Controller pattern, separating concerns to simplify development and improve maintainability.
Advantages
Supports multiple view technologies beyond JSP.
Integrates with the Spring IoC container and AOP.
Clear role distribution: DispatcherServlet, HandlerMapping, HandlerAdapter, ViewResolver.
Flexible request‑resource mapping strategies.
Core components
DispatcherServlet – front‑controller that receives all HTTP requests.
HandlerMapping – finds the appropriate controller based on the URL.
HandlerAdapter – invokes the controller.
Controller (Handler) – contains business logic; should be stateless to avoid thread‑safety issues.
ViewResolver – resolves logical view names to actual view implementations (JSP, Freemarker, etc.).
View – the rendered page.
Request processing flow
User sends a request to DispatcherServlet. DispatcherServlet obtains a Handler via HandlerMapping. DispatcherServlet calls the appropriate HandlerAdapter.
The Handler executes and returns a ModelAndView. HandlerAdapter passes the ModelAndView back to DispatcherServlet. DispatcherServlet forwards the model to ViewResolver, which selects the concrete View.
The view renders the model data and the response is sent to the client.
Transaction management
Spring provides programmatic (using TransactionTemplate) and declarative transaction management (using @Transactional with AOP). Declarative transactions support propagation behaviors (REQUIRED, REQUIRES_NEW, SUPPORTS, etc.) and isolation levels (READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE, etc.).
Common Spring MVC annotations @Controller – marks a class as a Spring MVC controller. @RequestMapping – maps URLs, HTTP methods, parameters, headers, consumes/produces media types. @PathVariable – binds URI template variables to method parameters. @RequestParam – binds request parameters to method arguments. @RequestBody – binds the request body (e.g., JSON) to a Java object. @ResponseBody – writes the return value directly to the HTTP response. @Autowired and @Resource – inject beans; @Autowired defaults to by‑type, @Resource defaults to by‑name. @ModelAttribute and @SessionAttributes – prepare model data before handler execution and store selected attributes in the HTTP session.
These annotations enable concise, type‑safe request handling and seamless integration with Spring’s IoC container.
Component scanning
Using <context:component-scan> with optional include-filter and exclude-filter allows selective registration of @Component, @Controller, @Service, and @Repository beans.
Comparison with Struts2
Spring MVC uses DispatcherServlet as the front controller; Struts2 uses a filter.
Spring MVC binds request data to method parameters (thread‑safe, singleton possible); Struts2 binds to action properties (prototype scope).
Spring MVC integrates naturally with the Spring ecosystem; Struts2 requires separate integration.
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.
Intelligent Backend & Architecture
We share personal insights on intelligent, automated backend technologies, along with practical AI knowledge, algorithms, and architecture design, grounded in real business scenarios.
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.
