Spring MVC (Model-View-Controller) is a web framework provided by Spring for building web applications in Java. It follows the MVC architectural pattern, which divides an application into three interconnected components: Model, View, and Controller.
- Represents the application's data and business logic.
- Encapsulates the state of the application and provides methods to manipulate that data.
- Typically consists of objects that represent entities in the application, such as users, products, orders, etc.
- Represents the presentation layer of the application.
- Responsible for rendering the user interface based on the data provided by the Model.
- Generates HTML output that is sent back to the client's web browser for display.
- Acts as an intermediary between the Model and the View.
- Receives incoming requests from the client, processes them, and determines how to respond.
- Implemented as Java classes annotated with
@Controller
or@RestController
. - Contains handler methods annotated with
@RequestMapping
that map specific URL patterns to business logic.
- Client Request: A request is triggered from the client and hits the configuration file known as
web.xml
(Deployment Descriptor). If there is no information present in theweb.xml
file, it tries to search for theDispatcherServlet
class. - DispatcherServlet Initialization: A custom initializer class, which must be a child of
AbstractAnnotationConfigDispatcherServletInitializer
, is used as a replacement for theweb.xml
file. TheDispatcherServlet
is responsible for two main jobs:- Invoking the Spring bean container and creating the respective objects for the mentioned classes in the
AppConfig
class. - Receiving the dynamic URL request from the UI and sending back the responses after processing the request.
- Invoking the Spring bean container and creating the respective objects for the mentioned classes in the
- Handler Mapping and Adapter: The
DispatcherServlet
usesHandlerMapper
andHandlerAdapter
to achieve the above-mentioned goals. TheHandlerMapper
contains information about all the handler methods present in the classes annotated with@Controller
. The respective handler details are sent back to theDispatcherServlet
, which then talks to theHandlerAdapter
and forwards the request to the respective Controller layer of the application. - Controller Processing: Once the data is received on the Controller layer, it is forwarded to other layers of the application to process the request.
- Response Handling: Once the data is processed and the response is ready to be sent back to the client, the Controller layer again takes the help of
HandlerAdapter
to reach theDispatcherServlet
. - View Resolution: The
DispatcherServlet
takes the help of the view resolver to get the information of the view technology file that needs to be rendered on the browser based on the request received and processed. After receiving the view technology file details, theDispatcherServlet
gives back the response to the client.
ModelAndView
is a class in Spring MVC used to encapsulate both model (data) and view (HTML or JSP file) in a single object. It allows the controller class methods to return the data and the name of the view as a response. We can provide the view details using the setViewName(String viewName)
method from the ModelAndView
class. We can set the name and value for the data that has to be transferred using the addObject(String name, Object value)
method from the ModelAndView
class.
This package contains the following components:
- AddStudent.jsp: A JSP file for adding student details.
- Controller Classes: Java classes annotated with
@Controller
to handle incoming requests. - Model Classes: Java classes representing the entities in the application.
- Configuration Classes: Java classes for configuring the Spring application context.
This JSP file provides a form for entering student details:
<!-- ...existing code... -->
<form action="add-student" method="post">
<input type="number" placeholder="Enter Student Id" name="studentId"> <br>
<input type="text" placeholder="Enter Student Name" name="studentName"> <br>
<input type="text" placeholder="Enter Student Email" name="studentEmail"> <br>
<input type="number" placeholder="Enter Student Age" name="studentAge"> <br>
<input type="number" placeholder="Enter Student Marks" name="studentMarks"> <br>
<input type="submit" value="ADD">
</form>
<!-- ...existing code... -->
This form submits the student details to the add-student
URL, which is handled by a controller class in the application.
- Client Request: The user fills out the form in
AddStudent.jsp
and submits it. - DispatcherServlet: The request is received by the
DispatcherServlet
, which is initialized by a custom initializer class. - Handler Mapping: The
DispatcherServlet
usesHandlerMapper
to find the appropriate controller method to handle the request. - Controller: The request is forwarded to the controller method mapped to the
add-student
URL. - Model Processing: The controller processes the request, interacts with the model to handle business logic, and prepares the data.
- ModelAndView: The controller returns a
ModelAndView
object containing the model data and the view name. - View Resolution: The
DispatcherServlet
uses the view resolver to determine the view technology file (e.g., JSP) to render. - Response: The view is rendered and the response is sent back to the client's web browser.