Core Components
-
Model:
Represents the data or business object.
It can also include logic to update the controller if the data changes.
Example: A
Studentclass with fields likenameandrollNo. - View: Represents the visualization of the data that the model contains. It is responsible for rendering the user interface. Example: A console printout, an HTML page, or a JSON response.
- Controller: Acts on both the Model and the View. It controls the data flow into the model object and updates the view whenever data changes. Example: A class that retrieves data from a database and passes it to the view.
Real-World Analogy
Think of a Restaurant:- The Customer (User) sits down and gives an order.
- The Waiter (Controller) takes the order from the customer and sends it to the kitchen.
- The Kitchen (Model) gathers the ingredients (data) and cooks the meal.
- The Waiter (Controller) takes the cooked meal from the kitchen.
- The Plate/Presentation (View) is how the food is arranged and served to the customer.
Implementation Example
Here is a raw Java implementation to demonstrate the pure architectural flow without any frameworks.1. Create the Model
TheStudent object serves as our data carrier.
Student.java
2. Create the View
TheStudentView is responsible for printing the details to the console.
StudentView.java
3. Create the Controller
TheStudentController acts as the intermediary.
StudentController.java
4. Demo Class
Simulating the user interaction.MVCPatternDemo.java
When to Use
- Web Applications: Almost all modern Java web frameworks (Spring MVC, Struts, JSF) are based on this pattern.
- Separation of Duties: When you have a team where UI designers work on the View (HTML/CSS) and backend engineers work on the Logic (Model/Controller).
- Multiple Views: When the same data needs to be displayed in different formats (e.g., HTML for a browser, JSON for a mobile app).
Pros & Cons
| Pros | Cons |
|---|---|
| Separation of Concerns: High cohesion and loose coupling. | Complexity: Can be overkill for very simple, small applications. |
| Simultaneous Development: Frontend and backend teams can work in parallel. | Efficiency: Frequent updates between views and models can degrade performance if not managed well. |
| Testability: Controllers and Models can be tested independently of the View. |
Spring Boot Context
In modern Java development, we rarely implement the MVC pattern from scratch using raw Java classes. Instead, we rely on Spring MVC, which is built on top of the Servlet API.How Spring Handles MVC
Spring introduces a special component called the DispatcherServlet. This acts as the Front Controller (another J2EE pattern) that intercepts all incoming requests and delegates them to the correct Controller.Key Annotations
-
@Controller:- Marks a class as a Spring MVC Controller.
- Typically used for traditional web applications where the response is a rendered HTML view (using Thymeleaf, JSP, etc.).
-
@RestController:- A convenience annotation that combines
@Controllerand@ResponseBody. - It tells Spring that the return value of the methods should be written directly to the HTTP response body (usually as JSON) rather than resolving to a View.
- This is the standard for building RESTful APIs.
- A convenience annotation that combines
Spring Boot Implementation Example
Here is how the previous “Student” example translates to Spring Boot code. The Model (Student.java):
Student.java
StudentController.java):
StudentController.java
@RestController scenario, the “View” is the JSON response sent to the client (e.g., React, Postman, or a mobile app).