Spring MVC (Model-View-Controller) is a part of the Spring Framework that provides a comprehensive and flexible web development framework. It is designed to simplify the development of web applications by providing a clear separation of concerns between the various layers of an application.
Core Components of Spring MVC
- DispatcherServlet:
- Acts as the front controller in the Spring MVC architecture.
- Handles all incoming HTTP requests and delegates them to the appropriate controllers based on the URL patterns defined.
- It is configured in the
web.xml
(if using XML configuration) or as a@Bean
in a@Configuration
class.
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
@Bean public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); }
- HandlerMapping:
- Maps incoming requests to appropriate handler methods in the controllers.
- Commonly used
HandlerMapping
implementations includeRequestMappingHandlerMapping
which maps requests based on@RequestMapping
annotations.
@RequestMapping("/hello") public String handleRequest() { return "hello"; }
- Controller:
- Contains methods to handle requests and return model and view information.
- Annotated with
@Controller
or@RestController
(for REST APIs). - Methods are annotated with
@RequestMapping
(or other request mapping annotations like@GetMapping
,@PostMapping
, etc.) to specify request URL patterns.
@Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, World!"); return "hello"; } }
- Model:
- Represents the data of the application.
- Passed to the view for rendering, usually through the
Model
orModelAndView
objects.
@GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, World!"); return "hello"; }
- View:
- Responsible for rendering the model data.
- Can be JSP, Thymeleaf, FreeMarker, or other view technologies supported by Spring.
- Defined in the view resolver configuration.
<!-- hello.jsp --> <html> <body> <h1>${message}</h1> </body> </html>
- ViewResolver:
- Maps logical view names to actual view implementations.
- Common implementations include
InternalResourceViewResolver
for JSPs andThymeleafViewResolver
for Thymeleaf templates.
@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
Request Handling Flow in Spring MVC
- Client Request: The client sends an HTTP request to the web application.
- DispatcherServlet: The request is received by the
DispatcherServlet
, which acts as the front controller. - Handler Mapping: The
DispatcherServlet
consultsHandlerMapping
to determine the appropriate controller method to handle the request. - Controller: The request is forwarded to the identified controller method.
- Business Logic: The controller processes the request, interacts with the service layer, and prepares the model data.
- Model and View: The controller returns a
ModelAndView
object, containing the model data and view name. - View Resolver: The
DispatcherServlet
consults theViewResolver
to resolve the logical view name to an actual view. - View Rendering: The view (e.g., JSP, Thymeleaf) is rendered with the model data and the response is sent back to the client.
Advanced Features
- Form Handling:
- Spring MVC simplifies form handling through the use of
@ModelAttribute
and@RequestParam
annotations. - The
BindingResult
object is used for validation and error handling.
@Controller public class FormController { @GetMapping("/form") public String showForm(Model model) { model.addAttribute("formData", new FormData()); return "form"; } @PostMapping("/form") public String submitForm(@ModelAttribute FormData formData, BindingResult result) { if (result.hasErrors()) { return "form"; } // Process formData return "result"; } }
- Spring MVC simplifies form handling through the use of
- Exception Handling:
- Centralized exception handling can be achieved using
@ControllerAdvice
and@ExceptionHandler
annotations.
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public String handleException(Exception ex, Model model) { model.addAttribute("errorMessage", ex.getMessage()); return "error"; } }
- Centralized exception handling can be achieved using
- Interceptors:
- Interceptors provide a way to intercept requests for pre-processing and post-processing.
- Implement the
HandlerInterceptor
interface and configure it inWebMvcConfigurer
.
public class LoggingInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // Pre-processing logic return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Post-processing logic } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // After completion logic } } @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoggingInterceptor()); } }
- RESTful Web Services:
- Spring MVC supports building RESTful web services using
@RestController
,@GetMapping
,@PostMapping
,@PutMapping
, and@DeleteMapping
annotations.
@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/resource") public ResponseEntity<Resource> getResource() { Resource resource = new Resource(); return ResponseEntity.ok(resource); } @PostMapping("/resource") public ResponseEntity<Void> createResource(@RequestBody Resource resource) { // Create resource logic return ResponseEntity.status(HttpStatus.CREATED).build(); } }
- Spring MVC supports building RESTful web services using
- Asynchronous Request Processing:
- Spring MVC supports asynchronous request processing to handle long-running tasks without blocking the main thread.
@RestController public class AsyncController { @GetMapping("/async") public Callable<String> asyncRequest() { return () -> { // Simulate long-running task Thread.sleep(2000); return "asyncResult"; }; } }
Conclusion
Spring MVC is a powerful and flexible framework for building web applications in Java. By providing a clear separation of concerns and a comprehensive set of tools and features, it simplifies the development of robust and maintainable web applications. Understanding the core components and advanced features of Spring MVC is essential for any Java web developer aiming to build scalable and efficient web applications.