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.
Diagram illustrating the logic flow in an MVC architecture.
MVC is also widely used when making regular console or desktop UI apps.
Summary:
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.
MVC Advantages
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.
Diagram illustrating the flow in Spring MVC, with a focus on behind-the-scenes components such as DispatcherServlet and the Handler Mapping and the View Resolver. Don’t mind the extra “Model”, all of these individual aspects will be explored in later topics.
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.
Summary
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.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duur
Omschrijving
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.