Spring Web MVC – The Model Object

In this tutorial, you will learn how to pass information from your Controller class to the View using the Spring MVC Model object.

A Model is an object that you can use to pass attributes from the Controller to the View.

To make the Model object available to a method in your controller class, you will simply inject it as an argument to your method.

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String homePage(Model model) {
  
        return "home";
    }

}

Adding Attributes to the Model

Let’s assume that we need to pass the user’s first name and last name from the Controller to the View. To do that, we can use the addAttribute() method that the Model provides.

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String homePage(Model model) {
        model.addAttribute("firstName","Sergey");
        model.addAttribute("lastName","Kargopolov");
        return "home";
    }

}

Access Model Attributes in the View

In the View, we can access and display attributes from the Model object using the ${attributeName}.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="'Hello ' + ${firstName} + ' ' + ${lastName}" />

</body>
</html>

Frequently Asked Questions

  1. How is the Model object created in Spring Web MVC?
    The Model object is automatically created by the Spring container when a controller method is invoked.
  2. How do I access data from the Model object in the view layer of my application?
    You can access data from the Model object in the view layer by using the Expression Language (EL) syntax. For example, ${name} would retrieve the value of an attribute named “name” from the Model object.
  3. Can I pass complex objects to the Model object in Spring Web MVC?
    Yes, you can pass complex objects to the Model object in Spring Web MVC. Spring will automatically convert them to their string representation using their toString() method.
  4. How long does the Model object last in Spring Web MVC?
    The Model object is only valid for the current request and is discarded once the response has been sent to the client.
  5. How do I unit test a controller that uses the Model object in Spring Web MVC?
    You can mock the Model object using a mocking framework like Mockito or EasyMock, and then verify that the controller method adds the expected data to the Model object.

I hope this tutorial was helpful for you. To learn more about building Web applications with Spring Framework, please visit the Spring Web MVC category.  You will find many useful tutorials there.

Happy learning!


Leave a Reply

Your email address will not be published. Required fields are marked *