Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
In Spring MVC, the Model
interface is central to how the MVC framework manages data that the controller intends to display in the view. The primary role of the Model
interface is to hold and manage the data that will be used in the view layer.
Data Storage: The Model interface allows you to add attributes, which are key-value pairs that the view can access.
View Agnostic: The Model is independent of the view layer, meaning you can use the same model with different views (like JSP, Thymeleaf, etc.).
Merge with ModelAndView: The attributes in the Model can be merged with the ModelAndView
object if required.
addAttribute(String attributeName, Object attributeValue)
: Adds an attribute to the model under the supplied name.
addAttribute(Object attributeValue)
: Adds an attribute to the model using its class name (decapitalized) as the name.
addAllAttributes(Map<String, ?> attributes)
: Adds all attributes contained in the supplied map.
containsAttribute(String attributeName)
: Checks if the model contains an attribute with the given name.
mergeAttributes(Map<String, ?> attributes)
: Merges the supplied attributes, not overriding any existing ones.
asMap()
: Returns the model as a map. This can be useful when we want to inspect all the model attributes.
Consider a simple example where a controller is used to display user details:
@Controller @RequestMapping("/user") public class UserController { @GetMapping("/details") public String userDetails(Model model) { User user = new User("John", "john@example.com"); model.addAttribute("user", user); return "userDetails"; } }
In the above code, the Model
interface is used to add a User
object to the model. The key here is "user"
, and the value is the User
object. In the corresponding view (userDetails.jsp
for example), you'd be able to access this object using the key.
In a JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <p>Name: <c:out value="${user.name}" /></p> <p>Email: <c:out value="${user.email}" /></p>
The Model
interface is typically used in the context of the controller methods. For more complex scenarios, you might use the ModelAndView
class which combines both the model and the view into a single object.
Spring MVC Model Interface Example:
Description: This is a basic example demonstrating the usage of the Model
interface in a Spring MVC controller to add attributes to the model.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/welcome") public String welcome(Model model) { model.addAttribute("message", "Welcome to Spring MVC!"); return "welcome"; } }
Using Model Interface in Spring MVC:
Description: This example shows how to use the Model
interface in a Spring MVC controller to add attributes that are passed to the view.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/greet") public String greet(Model model) { model.addAttribute("greeting", "Hello, user!"); return "greet"; } }
How to Work with Model in Spring MVC Controller:
Description: This example provides insights into working with the Model
interface in a Spring MVC controller to pass data to the view.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/showData") public String showData(Model model) { model.addAttribute("data", "This is some data."); return "dataView"; } }
Model Interface vs ModelAndView in Spring MVC:
Description: This example compares the usage of the Model
interface with ModelAndView
in a Spring MVC controller for handling data and views.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { // Using Model interface @RequestMapping("/modelExample") public String modelExample(Model model) { model.addAttribute("message", "Using Model Interface"); return "modelView"; } // Using ModelAndView @RequestMapping("/modelAndViewExample") public ModelAndView modelAndViewExample() { ModelAndView modelAndView = new ModelAndView("modelView"); modelAndView.addObject("message", "Using ModelAndView"); return modelAndView; } }
Passing Data to Views with Spring MVC Model:
Description: This example demonstrates the process of passing data to views in Spring MVC using the Model
interface.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/userInfo") public String userInfo(Model model) { User user = userService.getUserById(123); model.addAttribute("user", user); return "userInfoView"; } }
Model Interface in Spring MVC Explained:
Description: This example provides an explanation of the Model
interface in Spring MVC and its role in passing data from controllers to views.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/about") public String about(Model model) { model.addAttribute("info", "This is the about page."); return "aboutView"; } }
Adding Attributes to the Model in Spring MVC:
Description: This example illustrates how to add attributes to the Model
interface in a Spring MVC controller for use in the associated view.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @RequestMapping("/productDetails") public String productDetails(Model model) { Product product = productService.getProductById(456); model.addAttribute("product", product); return "productDetailsView"; } }
Working with Model in Thymeleaf Templates:
Description: This example showcases how to work with the Model
interface in Thymeleaf templates to access and display data passed from the controller.
Code Snippet: (Thymeleaf Template)
<!-- Thymeleaf Template --> <html> <body> <h2>Welcome to Thymeleaf Example</h2> <p th:text="${message}"></p> </body> </html>