Model-View-Controller (MVC) Design Pattern

Model-View-Controller (MVC) Design Pattern

The Model-View-Controller (MVC) design pattern is an architectural pattern that separates an application into three main components:

1. Model

  • Represents the application’s data and business logic.
  • Manages the data, logic, and rules of the application.
  • Notifies the view of any changes in the data.

2. View

  •  Represents the presentation layer (UI) of the application.
  •  Displays the data from the model to the user.
  •  Sends user actions to the controller.

3. Controller

  •  Acts as an intermediary between the model and the view.
  •  Receives user input from the view and processes it.
  •  Updates the model based on user input and logic.
  •  Determines which view to display based on the model’s state.

Model-View-Controller (MVC) Design Pattern

Project Structure
Java Example Using Spring MVC
 1. Project Structure
```
src
 └── main
     └── java
         └── com
             └── example
                 └── config
                     └── WebConfig.java
                 └── controller
                     └── HomeController.java
                 └── model
                     └── Message.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
package com.example.config;

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");
    }
}
```

Model (Message.java)
4. Model (Message.java)
```java
package com.example.model;

public class Message {
    private String content;

    public Message(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
```

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

import com.example.model.Message;
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) {
        Message message = new Message("Hello, Spring MVC!");
        model.addAttribute("message", message.getContent());
        return "home"; // returns the view name "home"
    }
}
```

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


web.xml Configuration
 7. 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