Loading

Spring MVC

  • Spring Web MVC framework provides model-view-controller(MVC) architecture and ready components that can be used to develop flexible web applications.

Key points:-

  • MVC pattern divides an application into input logic, business logic, and UI logic.
  • Spring MVC is an architecture that is based on the model-view-controller design principle.
  • A loosely coupled application is created by the MVC design pattern.  
  • In J2EE we use MVC design patterns to create dynamic websites.

Model View Controller Design Pattern:-

  • Model:- This is a class that encapsulates the application data. It is also called the POJO class(Plain old Java object). It is used to implement readability & reusability.
  • View:- It generates HTML output to render model data for interpretation by the client's browser.
  • Controller:-  IT is responsible for handling the Request and sending the response back to the client with processed application data.

Spring MVC project

For creating the Spring MVC project we use Maven:-

  • Apache Maven is a tool used for managing software projects and understanding them. It uses a Project Object Model (POM) concept to manage building, documentation, and reporting from a central information location. With Maven, we can easily build and manage any Java-based project.

Steps for Creating a Maven Project:-

  • Click on file ---> New
  • Click on Maven Project---> Next
  • select org.apache.maven.archetypes / maven-archetype-webapp /1.0 ---> Next
  • Enter group id: package name, Artifact id: project name, version: 0.0.1-SNAPSHOT
  • Finish.

Spring MVC Flow

Dispatcher Servlet:-

  • This is also called a front controller in the Spring web application. All the requests first arrived at the dispatcher servlet. 
  • It is defined in the web.xml.

Handler mapping:-

  • Dispatcher Servelet approach handler mapping for forwarding the request to the appropriate controller.

Controller:-

  •      It is a Java class. The requested URL is mapped with a method based on the HTTP methods    (Get, Post, Put, Head etc.). 
  •     This method returns the model and the view.  

View Resolver:-

  •  Based on the controller output the View Resolver foreword the view to dispatcherServlet.
  •  The DispatcherServlet invokes the model data to the view.
  •  Finally, the model-view-object is sent back as a response to the browser or the client. 

Advantages of Spring MVC  framework

  • It enables rapid and parallel development
  • Development of the application becomes fast
  • Easy for multiple developers to work together
  • Easier to update the application
  • And easier to debug because we have multiple levels in the application

Disadvantages of Spring MVC framework

  • It has high complexity to develop the application using the pattern.
  • It is not suitable for small applications which affects the application's performance and design.