Model in Spring MVC — Passing Data from Controller to View
Fast recall
Quick revision
Core answer
Understanding of how Spring MVC passes data from controller layer to presentation layer and the distinction between Model interface vs MVC pattern's Model layer
Confusing Spring's Model interface (a data carrier) with the conceptual Model layer in MVC architecture (services, repositories, business logic)
Clearly differentiate Spring's Model object (view data container) from the MVC pattern's Model layer (business logic) and explain how Model isn't needed in REST controllers because @RestController serializes return values directly to JSON
Interview-ready guide
Interview answer
A page controller can call model.addAttribute("user", userDto) and return "profile". A Thymeleaf template then reads ${user.name}. For an API, returning userDto from a RestController skips view resolution and writes a response representation. Both objects contain data, but only the first uses Spring's Model abstraction. Keeping the terms separate makes the request flow easier to understand.
Model in Spring MVC — Passing Data from Controller to View
Direct Answer
In Spring MVC, Model is a holder for attributes exposed to a view during server-side rendering. A controller adds values and returns a view name; the selected view reads those attributes. A Java object returned from a @RestController is not 'the Model' in this sense—it is a handler return value written to the response body by an HttpMessageConverter.
Code Example
// Traditional MVC controller - uses Model for view rendering
@Controller
public class ProductController {
@GetMapping("/products/{id}")
public String showProduct(@PathVariable Long id, Model model) {
var product = productService.findById(id);
model.addAttribute("product", product); // Add to Model
model.addAttribute("categories", categoryService.findAll());
return "product-details"; // View name - Thymeleaf template
}
}
// REST controller - no Model needed, return object directly
@RestController
@RequestMapping("/api/products")
public class ProductApiController {
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) {
return productService.findById(id); // Auto-serialized to JSON
}
}
// In Thymeleaf template (product-details.html):
// <h1 th:text="${product.name}">Product Name</h1>
// The ${product} accesses the Model attribute we added
Common Mistakes & Pitfalls
- Using Model in REST controllers: Freshers add Model parameters to
@RestControllermethods thinking they need it for JSON responses. Spring REST doesn't use views, so Model attributes are ignored — just return the object directly. - Confusing Model with Model layer: Saying "the Model handles business logic" mixes Spring's Model interface with the MVC architectural pattern's Model layer (services/entities). Spring's Model is just a data container.
- Overloading Model with services: Adding service objects to Model instead of the data they produce. The view needs data (a User object), not the UserService itself. Pass
model.addAttribute("user", userService.getUser()), not the service.
Check your understanding
Answer these without looking back at the article.
- What is the difference between Model and ModelAndView in Spring MVC?
- What does @ModelAttribute do in Spring MVC?
- How does data pass from controller to view in Spring MVC?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.