0% found this document useful (0 votes)
16 views3 pages

Spring MVC Pattern With Dispatcher Servlet

Uploaded by

bansodesangam23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Spring MVC Pattern With Dispatcher Servlet

Uploaded by

bansodesangam23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Spring MVC pattern with Dispatcher Servlet

Model: Represents the application's data or business logic. It manages the data and
interacts with the database.
View: Displays the data and is the UI layer of the application. It interacts with the model
and shows relevant information to the user.
Controller: Acts as an intermediary between the Model and View. It processes user
inputs and returns the appropriate response after interacting with the model.

Architecture:
1. Client (Browser or REST Client):
The client interacts with the server by sending HTTP requests. These can be GET, POST,
PUT, DELETE, etc., depending on the operation.
2. Controller Layer:
This is the entry point of the application for client requests.
Controllers are annotated with @Controller or @RestController and handle HTTP
requests using methods annotated with @RequestMapping, @GetMapping,
@PostMapping, etc.
The controller processes the incoming request, invokes services, and prepares the
appropriate response (HTML, JSON, XML).
3. Service Layer:

This layer contains the business logic of the application. It's responsible for processing
data and implementing business rules.
The service methods are usually called by the controller.
Services are defined using the @Service annotation.

4. Repository (DAO) Layer:


This layer handles database operations and is typically responsible for interacting with
the database.
It uses JPA (Java Persistence API) or other persistence technologies like JDBC,
Hibernate, etc.
Repositories are defined using @Repository or by extending Spring Data JPA interfaces
like JpaRepository.
5. Model Layer:
Represents the data or domain objects of the application.

Models can be simple POJOs (Plain Old Java Objects) with attributes and methods that
represent the data and state of the application.
Models interact with the database via the repository layer.
6. View Layer:

The view is responsible for displaying the data returned by the controller to the user.
In Spring Boot, this can be implemented using Thymeleaf, JSP, FreeMarker, or any front-
end framework like Angular, React (if you're using a REST API).
The view takes the model data and renders it into a format appropriate for the client
(HTML for web pages, JSON for REST APIs).

DispatcherServlet Workflow (Request-Response Lifecycle)


The detailed request lifecycle of DispatcherServlet involves multiple steps and
components that work together to process an HTTP request and generate the
corresponding response. Below is a breakdown of the steps:
Step 1: Receiving the Request
• A user sends a request (like an HTTP GET or POST request) to the server.
• This request is intercepted by the DispatcherServlet because it is mapped to
handle all incoming requests in the web.xml or automatically by Spring Boot in
application.properties.

Step 2: Request Mapping (Handler Mapping)


• The DispatcherServlet uses one or more HandlerMapping implementations to
determine which controller should handle the request.
• HandlerMapping uses the request URL, HTTP method, and other attributes to
match the request to the appropriate controller method (mapped using
annotations like @RequestMapping, @GetMapping, etc.).
Step 3: Request Handling (Controller)
• Once a matching controller method is found, the DispatcherServlet forwards
the request to that method for processing.
• The controller method processes the request parameters, interacts with the
business logic layer (service layer), and prepares the response data.
Step 4: View Resolution
• After processing the request, the controller typically returns a ModelAndView
object, which contains both the view name and the model data.
• The DispatcherServlet then consults the ViewResolver to resolve the logical
view name (e.g., home) into a physical view (e.g., home.jsp or home.html).
• If the request is part of a REST API, the controller may directly return data (like
JSON or XML) without rendering a view.
Step 5: Rendering the View
• The resolved view (e.g., a Thymeleaf template, JSP, or a JSON response) is
rendered by combining the model data with the view template.

• The ViewResolver (like InternalResourceViewResolver for JSP or


ThymeleafViewResolver for Thymeleaf) helps in mapping the view name to the
actual view file.
Step 6: Returning the Response

• Once the view is rendered, the DispatcherServlet returns the response (HTML,
JSON, etc.) to the client.

You might also like