MVC is used across the board, throughout various popular languages and technologies, as a way of separating concerns and organising our logic to more efficiently build up applications with graphical user interfaces, especially web apps.
So, on top of Spring Web’s Servlet API abstraction for basic web functionality, Spring MVC also offers out-of-the-box architectural support for things like views and controllers so we can get up and running with a well-oiled design pattern as quickly as possible.
MVC Pattern Explained
The Model-View-Controller (MVC) pattern is a design pattern that separates an application into three interconnected components, each with its distinct responsibilities:
Model: The Model (in this context) represents the application’s data and business logic. It is responsible for data retrieval, storage, and processing. In the context of a web application, the Model interacts with the database and processes data.
View: The View is responsible for presenting data to the user. It defines the user interface and how information is displayed. In web development, the View is typically the web page or template that users see.
Controller: The Controller handles user input, processes requests, and controls the flow of the application (think of a traffic cop or secretary). It communicates between the Model and the View, ensuring that data is retrieved and presented correctly.
MVC is also widely used when making regular console or desktop UI apps.
The 👁🗨View represents the web page or menu screen the user sees and interacts with directly (presentation output and user input). Buttons get clicked, forms are filled in and submitted, dynamically processed data is presented.
The 👮♂️Controller represents the redirecting, orchestrating traffic cop or secretary, who needs to process requests from the user in order to further process any input data, as well as provide the next requested view (i.e. which menu should be shown next).
It’s in charge of providing the view (i.e. web page template or menu screen) with the necessary data so it can be rendered on the screen, and then presents said view to the user.
The controller classes make use of data/business logic classes (e.g. service classes, repository classes, entity/model classes, etc.), aka the Model, to properly redirect incoming requests and process/fetch data, and then provides the relevant View with the necessary data to respond to the user with freshly rendered responses.
The ⚙️Model represents everything else; all the logic needed to process data (e.g. service layer), represent data (e.g. entity/model classes), store/retrieve data (e.g. repository layer). The Controller knows which systems to talk to in order to process/fetch the necessary data for the next requested view.
The MVC pattern offers several advantages, making it a popular choice for web development across different languages and technologies:
Separation of Concerns: The clear separation of Model, View, and Controller makes the codebase more organised and easier to maintain.
Reusability: Each component can be reused in different parts of the application, reducing code duplication.
Testability: Components can be tested independently, ensuring the reliability of the application.
Scalability: The modular architecture allows for the addition of new features or components without affecting existing functionality.
Flexibility: Changes to one component do not impact the others, providing flexibility and reducing the risk of unintended side effects.
How Spring MVC Works
Here is an example diagram representing the architecture of a typical Spring MVC application:
Spring MVC works by processing HTTP requests and generating responses based on the Model-View-Controller pattern. Here’s an overview of the typical flow of a Spring MVC application:
It all starts when a User Sends a Request: A user interacts with the web application by sending an HTTP request, typically through a web browser.
DispatcherServlet Receives the Request: The DispatcherServlet (shown below), a central component of Spring MVC behind the scenes, intercepts the incoming request. It acts as a front controller and is responsible for request processing.
Handler Mapping: The DispatcherServlet determines which controller should handle the request by using a Handler Mapping. The Handler Mapping maps the request to a specific controller method we as developers have written.
Controller Processes the Request: The selected controller method processes the request, which may involve invoking the Model to retrieve or manipulate data (in the example above, this involves using the services to process any incoming data, fetch any needed data, and smuggle it into the next view).
View Resolver: After processing, the controller selects the appropriate View for rendering the response (the view in our case is a webpage template that has data dynamically filled in). The View Resolver helps in locating the correct view template.
View Renders the Response: The View renders the response, presenting data to the user in the desired format, which is typically an HTML web page.
Response Sent to User: The DispatcherServlet sends the response back to the user through the web browser.
In Summary: A client sends an HTTP request, which gets processed by one of the methods in one of your Controller classes, and that method then (if necessary) uses your other (service/repository/entity/model) classes to process/fetch needed data, then returns a view (specifically an HTML web page template) as a HTTP response.
Note: Don’t worry too much about the small, behind-the-scenes details, just yet. You’ll understand these concepts once you’ve practised them a bunch, and then you’ll be reminded about the DispatcherServlet and other behind-the-scenes components.
Spring MVC is a part of Spring Web, used for building full-stack web applications.
It implements the Model-View-Controller (MVC) pattern, which separates an application into three components: Model (data and logic), View (user interface), and Controller (request handling).
MVC offers advantages like separation of concerns, reusability, testability, scalability, and flexibility.
Spring MVC processes HTTP requests with the DispatcherServlet, selects controllers using Handler Mapping, does any necessary coordination with the “Model” (service, repository, entity/model classes), and renders responses with views.
We gebruiken cookies op onze website om u de meest relevante ervaring te bieden door uw voorkeuren en herhaalde bezoeken te onthouden. Door op "Alles accepteren" te klikken, stemt u in met het gebruik van ALLE cookies. U kunt echter "Cookie-instellingen" bezoeken om een gecontroleerde toestemming te geven.