Building web applications requires handling HTTP requests, mapping URLs to handlers, parsing parameters, validating input, rendering views, and managing sessions. Without a structured framework, web-tier code becomes a messy mix of request handling, business logic, and presentation.
Spring MVC is a web framework built on the Model-View-Controller pattern, with the DispatcherServlet as the front controller. It cleanly separates concerns: Controllers handle requests, Models hold data, and Views render output. The framework handles request routing, parameter binding, validation, and view resolution transparently.
- DispatcherServlet: The front controller receives all HTTP requests and delegates to configured components
- Handler mapping: DispatcherServlet consults HandlerMapping to find which controller method handles the request URL
- Controller execution: The selected
@Controllermethod executes, processes the request, and returns a ModelAndView - View resolution: ViewResolver maps logical view names (e.g., “home”) to actual view templates (home.jsp, home.html)
- Response rendering: View renders the model data into HTML, JSON, or XML
- Front controller pattern: DispatcherServlet centralizes request handling
- Annotation-based:
@Controller,@RequestMapping,@RequestParam,@ModelAttribute - View technology agnostic: Works with JSP, Thymeleaf, FreeMarker, Mustache
- REST support:
@RestControllercombines@Controller+@ResponseBodyfor JSON/XML APIs - Data binding: Automatically binds request parameters to Java objects
- Validation:
@Valid+BindingResultfor declarative input validation
- Built from: Spring Framework — Spring MVC is built on Spring Core (IoC, DI)
- Built from: DispatcherServlet — The front controller is the entry point of Spring MVC
- Related: Spring Controller — @Controller methods handle specific request mappings
- Builds into: Spring Boot — Spring Boot auto-configures Spring MVC with embedded Tomcat
- Related: Spring Form Handling — Form handling and validation in Spring MVC
- Contrasts with: JSP — JSP is a view technology; Spring MVC is a full web framework
- Hidden HttpMethod: HTML forms only support GET/POST; Spring’s
HiddenHttpMethodFilterconverts_method=PUTto actual PUT - @ModelAttribute vs @RequestParam: @ModelAttribute binds complex objects; @RequestParam binds single parameters
- ViewResolver chaining: Multiple ViewResolvers with order priority — first match wins
- async requests: DeferredResult and Callable for long-lived async processing in controllers