Explain Spring MVC Architecture

Explain Spring MVC Architecture

Spring MVC is a framework that follows the Model-View-Controller (MVC) design pattern. This pattern helps in separating the business logic, presentation logic, and navigation logic.

Components of Spring MVC Architecture:

1. DispatcherServlet: Acts as the front controller, receiving all incoming HTTP requests and dispatching them to appropriate handlers (controllers).

2. Controller: Handles user requests, interacts with the model, and returns a view.

3. Model: Represents the application data and business logic. It is typically comprised of POJOs (Plain Old Java Objects).

4. View: Responsible for rendering the model data. It can be JSP, Thymeleaf, or any other view technology.

5. HandlerMapping: Maps HTTP requests to handler methods of controllers.

6. ViewResolver: Resolves view names to actual view pages.

Spring MVC Workflow

  1. The client sends an HTTP request to the server.
  2. The request is received by the DispatcherServlet.
  3. DispatcherServlet consults the HandlerMapping to find the appropriate controller for the request.
  4. The controller processes the request, interacts with the Model, and returns the name of a View.
  5. DispatcherServlet consults the ViewResolver to get the actual view page.
  6. The view is rendered and the response is sent back to the client.

Spring MVC Architecture

Project Structure
Java Example
 1. Project Structure
```
src
 └── main
     └── java
         └── com
             └── example
                 └── controller
                     └── HomeController.java
     └── resources
         └── application.properties
     └── webapp
         └── WEB-INF
             └── views
                 └── home.jsp
     └── web.xml
```


Maven Dependencies (pom.xml)
 2. Maven Dependencies (pom.xml)
```xml
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.10</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
```

Web Configuration (WebConfig.java)
 3. Web Configuration (WebConfig.java)
```java
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/views/", ".jsp");
    }
}
```

Controller (HomeController.java)
 4. Controller (HomeController.java)
```java
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Hello, Spring MVC!");
        return "home"; // returns the view name "home"
    }
}
```

View (home.jsp)
 5. View (home.jsp)
```jsp
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>
```


web.xml Configuration
 6. web.xml Configuration
```xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
         version="4.0">
    
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Homepage

Readmore