MVC Architecture
What is MVC?
MVC helps in:
Organizing code better
Improving testability
Making development faster
Separating concerns
Basic Structure:
+---------------+
| View | ←--- UI (HTML, JSP, Thymeleaf, etc.)
+---------------+
↑
|
↓
+---------------+
| Controller | ←--- Handles input, routes it
+---------------+
↑
|
↓
+---------------+
| Model | ←--- Business logic + Data
+---------------+
Components of MVC
A. Model – "Data and Business Logic"
Responsible for data, business rules, and database interaction.
Holds data that is displayed on the view.
Ex: Entity classes, Repositories, Service layer.
In Spring Boot:
@Entity
public class Student {
@Id
private Long id;
private String name;
private String email;
// getters and setters
}
View – "User Interface"
This is what the user sees (HTML, JSP, Thymeleaf, etc.).
It displays the data received from the controller/model.
Does not contain business logic.
In Spring Boot:
JSP / Thymeleaf templates (.html)
<h1>Hello, ${user.name}</h1>
Controller – "Handles User Requests"
Acts as the middleman between View and Model.
Accepts input (HTTP request), processes it (via services), and returns response
(View or JSON).
Uses annotations like @RestController, @GetMapping, etc.
In Spring Boot:
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService service;
@GetMapping("/{id}")
public ResponseEntity<Student> getStudent(@PathVariable Long id) {
return ResponseEntity.ok(service.getStudentById(id));
}
}
Flow of MVC in Web Application
User Action (Click/Form/Request)
↓
Controller (Handles request)
↓
Model (Processes data, interacts with DB)
↓
Controller (Returns Model data)
↓
View (Renders response to user)
Spring Boot + MVC Flow
@Controller or @RestController → handles the web request
Service layer (@Service) → processes business logic
Repository layer (@Repository) → interacts with the database
View (Thymeleaf/JSP) → displays data
Real-World Example: Student Management
Model:
@Entity
public class Student {
@Id private Long id;
private String name;
}
Repository:
public interface StudentRepository extends JpaRepository<Student, Long> {}
Service:
@Service
public class StudentService {
@Autowired private StudentRepository repo;
public Student getStudentById(Long id) {
return repo.findById(id).orElseThrow();
}
}
Controller:
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired private StudentService service;
@GetMapping("/{id}")
public Student get(@PathVariable Long id) {
return service.getStudentById(id);
}
}
Benefits of MVC
| Benefit | Description |
| ---------------------- | --------------------------------------------- |
| Separation of Concerns | Easy to manage and develop each layer |
| Testability | Each component can be tested independently |
| Maintainability | Easy to modify code without affecting others |
| Reusability | Logic (Model) is reused across multiple views |
MVC vs REST
| Feature | MVC | REST
|
| ----------- | ----------------------------- |
------------------------------------- |
| Return Type | View (HTML, JSP, Thymeleaf) | JSON/XML (for frontend apps)
|
| Usage | Server-side rendered web apps | APIs consumed by frontend/mobile
apps |
| Controller | `@Controller` | `@RestController`
|
Summary
| Component | Responsibility |
| ---------- | ------------------------------------------ |
| Model | Business logic + database interaction |
| View | Presentation layer |
| Controller | Accepts input, processes, returns response |