Loading
What is Spring MVC?

Spring MVC is a part of the Spring Framework used to build dynamic, flexible, and loosely coupled web applications using the Model-View-Controller (MVC) architectural pattern.



MVC Pattern Basics

LayerRole
ModelData logic - POJO classes that hold business data
ViewUI logic - usually JSP, Thymeleaf, or HTML to show data
ControllerInput logic - Java class that receives request and returns response



Key Point

  • Clean separation of concerns (input, business logic, UI)
  • Makes code reusable and readable
  • Easy to manage large-scale applications
  • Supports dependency injection, AOP, and RESTful APIs



How to Create a Spring MVC Project Using Maven


Step 1: Open IDE ( Eclipse or Intellij )

Go to File --> New --> Maven Project


Step2: Select Archetype

Choose: org.apache.maven.archetypes:maven-archetype-webapp:1.0


Step3: Enter Details

  • Group Id: Your package name (e.g., com.quipoin)
  • Artifact Id: Project name (e.g., springmvcdemo)
  • Version: Default 0.0.1-SNAPSHOT


Step4: Click Finish

You now have a basic web structure created.



Spring MVC Flow Diagram 

1. Client Sends Request - DispatcherServlet

  • Acts as the Front Controller
  • Defined in web.xml


2. DispatcherServlet- Handler Mapping

Checks which Controller class/method should handle the request


3. Handler Mapping - Controller Method

Controller executes logic and returns ModelAndView object


4. Controller - View Resolver

Finds which View (JSP/HTML) to render


5. DispatcherServlet - View

Injects model data into the view and sends response to browser



Example: Web.xml Configuration ( to define DispatcherServlet)

<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>



Advantages of Spring MVC

  • Easy to update and test individual components 
  • Supports parallel development ( UI team + backend team )
  • Flexible configuration ( XML + Annotation-based )
  • Strong integration with Spring features like AOP, DI etc.


Disadvantages

  • High initial setup complexity
  • Not ideal for small/simple web applications
  • Slight performance overhead in very lightweight projects