# nguyenngocthanhsang/spring-mvc-crud-demo architecture

> Architecture diagram of the GitHub repository nguyenngocthanhsang/spring-mvc-crud-demo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nguyenngocthanhsang/spring-mvc-crud-demo
- Repository: https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo

## Overview

##### Project Type and Purpose
- This is a simple full-stack web application built with Spring MVC. Its primary purpose is to demonstrate CRUD operations (Create, Read, Update, Delete) on Student records using an in-memory data store and JSP views. It follows the classical MVC pattern with Java-based configuration and no external database.

##### Main Architectural Components
a. Client Layer
- Browser/Client that issues HTTP requests (e.g., GET /student/list, POST /student/save).
b. Web Server & Dispatcher
- Embedded Jetty (invoked via `mvn jetty:run`) or any Servlet container.
- `DispatcherServlet` (registered by `AppInitializer`) receives all incoming requests.
c. Configuration
- `AppInitializer.java` – replaces `web.xml`, bootstraps Spring’s DispatcherServlet.
- `AppConfig.java` – configures ViewResolver, static resource handling, component scanning.
d. Controller Layer (`com.group10.controller`)
- `HomeController` – handles root and home page requests.
- `StudentController` – handles CRUD endpoints (`list`, `showFormForAdd`, `showFormForUpdate`, `saveStudent`, `delete`).
e. Service/DAO Layer (`com.group10.dao`)
- `StudentDAO` interface defines CRUD signatures.
- `StudentDAOImpl` – in-memory implementation using `ArrayList<Student>`.
f. Model Layer (`com.group10.model`)
- `Student` – simple POJO with fields `id`, `firstName`, `lastName`, `email`, `dateOfBirth`.
g. View Layer (`src/main/webapp/WEB-INF/view`)
- JSP files (`home.jsp`, `list-students.jsp`, `student-form.jsp`) using Spring Form Tags and JSTL to render HTML.
h. Static Resources (`/resources/css/style.css`)
- Served via resource handler defined in `AppConfig`.

##### Relationships and Data Flow
1. Client Browser → HTTP Request → Jetty Container → DispatcherServlet
2. DispatcherServlet → HandlerMapping → identified Controller method
3. Controller → invokes `StudentDAO` methods (injected via `@Autowired`)
4. DAO → updates in-memory `ArrayList<Student>`
5. Controller → populates Spring `Model` with data, returns view name (`list-students`)
6. ViewResolver → resolves JSP path
7. JSP → renders HTML/CSS, sends HTTP Response back to Client

##### Architectural Patterns and Principles
- MVC (Model-View-Controller) separation.
- Java-based configuration (no XML) using `@Configuration` + `@EnableWebMvc`.
- Dependency Injection with `@Autowired`.
- Annotation-driven request mapping (`@Controller`, `@RequestMapping`).
- PRG (Post/Redirect/Get) pattern for form submission.
- In-memory data store for demo simplicity.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        Browser("Browser/Client"):::client
    end

    %% Web Server & Dispatcher
    subgraph "Web & Servlet Container"
        direction TB
        Jetty("Jetty Servlet Container"):::infra
        Dispatcher["DispatcherServlet\n(Spring MVC 6.1.6\nJakarta Servlet 6.0.0)"]:::web
    end

    Browser -->|"HTTP Request\n(GET/POST)"| Jetty
    Jetty -->|"/"| Dispatcher

    %% Configuration
    subgraph "Configuration (Java-based)" 
        direction TB
        AppInit["AppInitializer"]:::config
        AppCfg["AppConfig"]:::config
    end
    AppInit -->|-bootstraps-| Dispatcher
    AppCfg -->|configures\nViewResolver\nResourceHandler| Dispatcher

    %% Controller Layer
    subgraph "Controller Layer" 
        direction TB
        HomeCtrl["HomeController"]:::web
        StudentCtrl["StudentController"]:::web
    end
    Dispatcher -->|HandlerMapping| HomeCtrl
    Dispatcher -->|HandlerMapping| StudentCtrl

    %% Service/DAO Layer
    subgraph "Service/DAO Layer"
        direction TB
        DAOInterface["StudentDAO\n(interface)"]:::service
        DAOImpl["StudentDAOImpl"]:::service
    end
    StudentCtrl -->|"@Autowired\ncalls CRUD"| DAOInterface
    DAOInterface -->|implementation| DAOImpl

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DB[(In-Memory ArrayList<Student>)]:::data
        Model["Student POJO"]:::model
    end
    DAOImpl -->|"updates/invokes"| DB
    DB -->|"returns Student objects"| Model
    Model -->|"populates Model"| StudentCtrl

    %% View Layer
    subgraph "View Layer"
        direction TB
        ViewResolver["ViewResolver"]:::web
        JSPHome["home.jsp"]:::view
        JSPList["list-students.jsp"]:::view
        JSPForm["student-form.jsp"]:::view
        StaticHandler["StaticResourceHandler\n(resources/css)"]:::web
        CSS["style.css"]:::static
    end
    StudentCtrl -->|"returns view name"| ViewResolver
    ViewResolver -->|resolves to JSP| JSPHome
    ViewResolver -->|resolves to JSP| JSPList
    ViewResolver -->|resolves to JSP| JSPForm
    StaticHandler -->|serves| CSS

    JSPHome -->|"HTML/CSS"| Browser
    JSPList -->|"HTML/CSS"| Browser
    JSPForm -->|"HTML/CSS"| Browser
    CSS -->|applied styles| Browser

    %% Click Events
    click AppInit "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/config/AppInitializer.java"
    click AppCfg "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/config/AppConfig.java"
    click HomeCtrl "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/controller/HomeController.java"
    click StudentCtrl "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/controller/StudentController.java"
    click DAOInterface "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/dao/StudentDAO.java"
    click DAOImpl "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/dao/StudentDAOImpl.java"
    click Model "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/java/com/group10/model/Student.java"
    click JSPHome "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/WEB-INF/view/home.jsp"
    click JSPList "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/WEB-INF/view/list-students.jsp"
    click JSPForm "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/WEB-INF/view/student-form.jsp"
    click CSS "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/resources/css/style.css"
    click web.xml "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/WEB-INF/web.xml"
    click index.jsp "https://github.com/nguyenngocthanhsang/spring-mvc-crud-demo/blob/main/src/main/webapp/index.jsp"

    %% Styles
    classDef client fill:#E3F2FD,stroke:#2196F3,color:#0D47A1
    classDef infra fill:#ECEFF1,stroke:#607D8B,color:#263238
    classDef config fill:#CFD8DC,stroke:#455A64,color:#263238
    classDef web fill:#BBDEFB,stroke:#2196F3,color:#0D47A1
    classDef service fill:#C8E6C9,stroke:#388E3C,color:#1B5E20
    classDef data fill:#FFE0B2,stroke:#FB8C00,color:#E65100
    classDef model fill:#FFF9C4,stroke:#FBC02D,color:#F57F17
    classDef view fill:#F3E5F5,stroke:#9C27B0,color:#4A148C
    classDef static fill:#E1BEE7,stroke:#8E24AA,color:#4A148C
```
