Spring MVC Behind The Scenes#
Components Of Spring MVC Application#
- There are 3 main components of a Spring MVC Application:
- A set of web pages to layout UI components.
- A collection of Spring beans (controllers, services, etc).
- Spring configuration (XML, Annotations or Java).
How Does Spring MVC Work?#
- Let's see the diagram of a Spring MVC Application below then we will go to details for every components.
Front Controller#
- So the
Front Controller
known asDispatcherServlet
.- It is a part of the Spring Framework.
- It already developed by Spring Develop Team.
- It incoming HTTP requests and delegate them to the the appropriate controller for processing.
- When we work with Spring MVC, we don't need to create it anymore. We will focus on creating:
- Model Objects
- View Templates
- Controller Classes
DispatcherServlet#
- The
DispatcherServlet
is the key component of the Spring MVC web framework. It is a servlet that receives incoming HTTP requests and then processes them through a series of handlers, which ultimately generate a response that is sent back to the client. The DispatcherServlet is responsible for managing the entire lifecycle of a request, including handling request mapping, view resolution, and exception handling. It also manages the flow of control between multiple handler components, such as controllers and view resolvers, and provides a unified interface for processing both synchronous and asynchronous requests. - You can view more here
Controller#
Controller
is the code that is created by developer. It contains our business logic like:- Handle the request.
- Store / retrieve data (database, web service...).
- Place data in model then send it to the appropriate view template.
Model#
- The
Model
contains our data, so when our controller is executed and performs an operation to retrieve data from back-end systems like database, web services or Spring Bean. Then we can take that data and place it in into theModel
. So theModel
is like our container and we can ship it between various parts in our Spring MVC application and it will be passed over theView Template
.
View Template#
- The Spring MVC is flexible, it supports many view templates and there are two most view templates are:
- JSP (Jave Server Page).
- JSTL (JSP Standard Tag Library)
- So When the
Model
data comes to our view template then our JSP page can read that model data and display it.
- In case that we don't want to use the JSP template, we can make use of some other templates like Thymeleaf, Groovy, Velocity, Freemarker etc.
- You can view more here