You are on page 1of 21

Model-View-Controller

using Spring MVC


- Chapter 4
Problem area

Mixing application logic and markup is bad practice


● Harder to change and maintain
● Error prone
● Harder to reuse
Problem area
Model View Controller (MVC) Framework

● Spring MVC (Model View Controller) is a well-known and widely used


framework for developing web applications with loosely coupled and well
organised layering of presentation, persistence and controller layer.
● With a layered flexible architecture of Spring MVC, it becomes very
convenient for developers to isolate the bricks and pieces and combine them
together in the form of a professional web application.
● A web application developed following MVC standard is easier to scale,
update and manage as a loosely coupled layered web application is much
easier to modify to introduce any changes at a certain layer.
Model View Controller (MVC) Framework

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 7 : After receiving ModelAndView object from the controller, Dispatcher


Servlet now sends model object to view resolver to get the name of the view
which needs to be rendered.
Step 8 : Once the view to be rendered has been identified, Dispatcher Servlet
passes model object to the view. Model object contains the data which needs to
be displayed in the view. View will be rendered with the model data. Views can
be designed in any front-end technology.
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

● Business code: It provides reusable business codes that allow the


developer to use existing business objects instead of creating new objects.
● Easy testing: Spring generally uses Java Beans that allows the developer to
inject data using easy methods.
● Mapping: It provides flexible mapping that allows the page to redirect
easily.
● Dependency Injection: Inversion of Control or dependency injection
allows the developer to not create a complete environment for the
dependencies.
Project Structure
Spring MVC Example

Index.jsp
Spring MVC Example

Web.xml
Spring MVC Example

spring-servlet.xml
Spring MVC Example
MultiplicationController.java
Spring MVC Example

result.jsp

You might also like