Spring Web MVC

yash rajyash raj
3 min read

Introduction

There are two ways to develop java based web applications which are Model 1 architecture and Model 2 architecture.

Model 1 architecture

  • In this architecture, either servlet or JSP is used as the main component of the web application

  • If a servlet component is used then jsp won't be used or vice versa.

  • Each component contains multiple logic.

  • It is not suitable for large-scale applications

Because of these drawbacks, a new architecture called MVC 2 architecture was introduced

Model 2 architecture

  • In this architecture, we take support from multiple technologies in multiple layers to develop the logic.

  • It is more suitable for medium and large-scale applications.

  • MVC stands for Model View Controller respectively.

Spring MVC Components

There are six components in spring mvc:-

  1. Front Controller (dispatcher servlet)

  2. Handler Mapper

  3. Controller

  4. Model and View

  5. View resolver

  6. View

The flow of spring web mvc

  • Programmer deploys SpringMVC/SpringBootMVC application in webserver or web apps.

  • Deployment activities take place which involve IOC container creation, DispatcherServlet registration with a servlet container, Pre Instantiation of singleton scope spring beans like controller class, handler mapping, view resolvers, Service class, Dao class etc. In the meantime necessary dependency injection also takes place.

  • The browser gives a request to deploy the Spring MVC application.

  • The front controller traps the request and applies the common system services on the request like logging, tracking, auditing etc

  • Dispatcher servlet hands over the request to handler mapping to map incoming request with handler method of handler class and gets the request mapping object from handler mapping component having controller class bean id and handler method signatures.

  • Dispatcher servlet takes controller bean class id from the received request mapping object and gets the controller class object from the dispatcher servlet created IOC container dispatcher servlet also prepares the necessary objects based on the method signature of the handled method collected from request mapping info object.

  • Dispatcher servlet calls handler method having necessary object as the arguments on the above received controller class object.

  • The handler method of the controller class either directly processes the request or takes the support of service/DAO and keeps the result in a scope.

  • The handler method of the controller class returns the logical view name back to the dispatcher servlet.

  • The Dispatcher servlet gives the logical view name to view resolver.

  • View resolver map logical view name to physical view component and returns view object having physical view name and location.

  • Dispatcher servlet gets the physical view name and location from received view object and send the control to physical view component . This formatted result goes back to dispatcher servlet.

  • Dispatcher servlet sends the result back to browser as response.

The function of each Component

  • In Spring MVC applications DispatcherServlet acts as Front Controller it regulates all the flow of applications. This is the component that receives the request from the browser.

  • Once the request is received it is then passed to the handler mapper which identifies the request handler and the data is passed to the controller.

  • The controller will then process the request and return the model data and logical view name

  • View resolver will then identify the view file (location and extension) prefix and suffix.

  • View Component will render model data and view file.

  • The Dispatcher servlet will then send a response to the client in the client's understandable format

MVC2 Architecture Pros and Cons

Pros

  • There is a clear-cut separation between logic.

  • The modification done in one layer does not affect the other.

  • Maintainance of the project becomes easy.

  • Parallel development is possible so productivity is good.

  • It is nowadays industry standard to develop medium and large-scale applications in this architecture.

Cons

  • Since there is parallel development we need more programmers.

  • Knowledge of multiple technologies is required.

0
Subscribe to my newsletter

Read articles from yash raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

yash raj
yash raj