Spring MVC

Spring MVC is a powerful framework within the Spring ecosystem designed to simplify the development of web applications. It leverages the Model-View-Controller (MVC) design pattern to provide a structured approach to handling HTTP requests and responses. As a part of the larger Spring Framework, Spring MVC is often used in conjunction with Spring Boot, which provides automatic configuration of many components, allowing developers to focus on writing business logic.

Overview

Spring MVC integrates tightly with the Spring IoC (Inversion of Control) container, offering a simplistic configuration for building robust and scalable web applications. It allows developers to map HTTP requests to controller actions using annotations such as @Controller and @RequestMapping. This mapping facilitates the handling of requests and the rendering of views, making it easier to develop web applications.

DispatcherServlet

The DispatcherServlet is the central component of the Spring MVC framework, acting as the front controller. It receives all incoming HTTP requests and delegates them to the appropriate components for processing. The sequence of steps executed by the DispatcherServlet to process incoming requests is crucial for understanding the flow of a Spring MVC application.

Figure B.1 Spring MVC components. The DispatcherServlet is the primary component, and it delegates the request to various other components. Figure B.1 Spring MVC components. The DispatcherServlet is the primary component, and it delegates the request to various other components.

LocaleResolver

The LocaleResolver interface allows the DispatcherServlet to automatically resolve messages based on the client’s locale configuration. For all incoming requests, the DispatcherServlet asks the configured LocaleResolver implementation to resolve the locale and set it in the HttpServletResponse. The default implementation is AcceptHeaderLocaleResolver.

Figure B.2 Spring MVC LocaleResolver class hierarchy. The default implementation is AcceptHeaderLocaleResolver. Figure B.2 Spring MVC LocaleResolver class hierarchy. The default implementation is AcceptHeaderLocaleResolver.

HandlerExceptionResolver

The HandlerExceptionResolver interface is used to resolve exceptions that occur during handler mapping or execution. When an exception is thrown, the DispatcherServlet delegates to a chain of HandlerExceptionResolver implementations. These resolvers can handle the exception by returning a ModelAndView pointing to an error view, or by allowing subsequent resolvers to attempt handling it.

Web Application Design Patterns

Spring MVC supports various web application design patterns, including the use of a complete Java-based technology stack without JavaScript-based frontend frameworks. This pattern involves using frontend template engines like Thymeleaf, FreeMarker, or Mustache. Thymeleaf is particularly popular and widely used with Spring Boot applications.

Figure B.5 Web application design pattern with Spring MVC and Thymeleaf Figure B.5 Web application design pattern with Spring MVC and Thymeleaf

HiddenHttpMethodFilter

The HiddenHttpMethodFilter is a feature in Spring Boot applications that enables support for HTTP methods like PUT, PATCH, and DELETE, which are not natively supported by browsers. This is achieved by adding a hidden form field (_method) in HTML forms to indicate the actual HTTP method.

Figure B.6 Spring MVC-based Web application Thymeleaf flow diagram Figure B.6 Spring MVC-based Web application Thymeleaf flow diagram

Spring MVC Architecture

The architecture of Spring MVC is built around several key components, including the dispatcher servlet and handler mapping. These components work together to manage incoming requests and direct them to the appropriate controller actions. The dispatcher servlet acts as the central point for receiving HTTP requests and dispatching them to the appropriate handlers.

Figure 7.18 The Spring MVC architecture. In the diagram, you find the main components of Spring MVC. These components and the way they collaborate are responsible for a web app’s behavior. The controller (shaded differently) is the only component you implement. Spring Boot configures the other components. Figure 7.18 The Spring MVC architecture. In the diagram, you find the main components of Spring MVC. These components and the way they collaborate are responsible for a web app’s behavior. The controller (shaded differently) is the only component you implement. Spring Boot configures the other components.

In this architecture, the controller is highlighted as the component that developers implement. The other components, such as the dispatcher servlet, are configured by Spring Boot, allowing developers to focus on the business logic of their applications. The dispatcher servlet is responsible for returning the rendered view in the HTTP response, completing the request-response cycle.

Book TitleUsage of spring mvcTechnical DepthConnections to Other ConceptsExamples UsedPractical Application
Spring Start HereDiscusses Spring MVC as a framework for simplifying web application development, focusing on controller actions and request mapping. moreCovers the architecture of Spring MVC, including dispatcher servlet and handler mapping. moreHighlights integration with Spring Boot for autoconfiguration of components. moreProvides examples of implementing web applications with controller actions and HTML documents. moreFocuses on the business logic implementation, with Spring Boot handling configuration. more
Spring Boot in PracticeDescribes Spring MVC as a module implementing the MVC design pattern, integrated with Spring IoC. moreDetails components like DispatcherServlet, LocaleResolver, and HandlerExceptionResolver. moreDiscusses design patterns, including Java-based stacks and template engines like Thymeleaf. moreIllustrates design patterns with diagrams and examples of using Thymeleaf and HiddenHttpMethodFilter. moreEmphasizes robust and scalable web applications, with features like HTTP method support. more

FAQ (Frequently asked questions)

How is Spring MVC integrated with the Spring Framework?

What does Spring MVC provide for building web applications?

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest