Model-View-Controller Using Spring MVC
Model-View-Controller Using Spring MVC
Model
● The Model component corresponds to all the data-related logic that the user
works with.
● This can represent either the data that is being transferred between the View
and Controller components or any other business logic-related data.
● For example, a Customer object will retrieve the customer information from
the database, manipulate it and update it data back to the database or use it
to render data.
Model View Controller (MVC) Framework
View
● The View component is used for all the UI logic of the application.
● For example, the Customer view will include all the UI components such as
text boxes, dropdowns, etc. that the final user interacts with.
Model View Controller (MVC) Framework
Controller
● Controllers act as an interface between Model and View components to
process all the business logic and incoming requests, manipulate data using
the Model component and interact with the Views to render the final output.
● For example, the Customer controller will handle all the interactions and
inputs from the Customer View and update the database using the Customer
Model. The same controller will be used to view the Customer data.
Flow of requests in a Spring MVC application
Flow of requests in a Spring MVC application
Step 1 : Client requests for a page by specifying the Web URL for the page. E.g.
https://www.psgtech.edu/
Step 2 : Client request is intercepted by the Dispatcher Servlet also known as
Front Controller. Dispatcher Servlet is a servlet specified in Web.XML file (for
XML Based configurations) or in the Web Configuration class (for java based
configurations).
Flow of requests in a Spring MVC application
Step 3 : Dispatcher Servlet uses URL Mapping Handler to find out the relevant
controller class to which request should be passed for subsequent processing.
For example, If you have a Controller defined for all requests by specifying “/” in
the URL, all requests will be entertained by that controller.
Step 4 : Once Dispatcher Servlet has identified the Controller to be considered,
it passes the client request to the controller.
Flow of requests in a Spring MVC application
Step 5 : The controller class is the main class controlling the business logic flow
once request has been dispatched it it by dispatcher servlet. This class will
implement the methods for different type of http requests (e.g. GET, POST) .
Step 6 : The controller class will also be responsible for returning the
ModelAndView object back to the dispatcher servlet after getting all business
logic executed and any data returned from DAO layer. ModelAndView object
returned by the controller back to the controller specifies both view and model
objects. Controller class is annotated by @Controller annotation.
Flow of requests in a Spring MVC application
Step 9 : This view is returned to the client and client can see the view and
associated data on his browser.
Advantages of Spring MVC
● Different roles: The Spring MVC has separate roles which can be fulfilled
with the help of a specialized object. The roles involved in this are command
object, model object, controller, dispatcher servlet, view resolver, validator,
and more.
● Lightweight: The container used for the development and deployment of
applications uses a lightweight servlet.
● Fast development: The MVC spring framework enables rapid and parallel
development. It helps the developer to complete the project on time.
Advantages of Spring MVC
Index.jsp
Spring MVC Example
Web.xml
Spring MVC Example
spring-servlet.xml
Spring MVC Example
MultiplicationController.java
Spring MVC Example
result.jsp