Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where exactly is a model object created in Spring MVC?

After going through some tutorials and initial document reading from the docs.spring.org reference I understood that it is created in the controller of a POJO class created by the developer. But while reading this I came across the paragraph below:

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model. Once present in the model, the argument's fields should be populated from all request parameters that have matching names. This is known as data binding in Spring MVC, a very useful mechanism that saves you from having to parse each form field individually.

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
   
}

Spring Documentation

In the paragraph what is most disturbing is the line:

"If not present in the model ... "

How can the data be there in the model? (Because we have not created a model - it will be created by us.)

Also, I have seen a few controller methods accepting the Model type as an argument. What does that mean? Is it getting the Model created somewhere? If so who is creating it for us?

like image 332
JAVA Avatar asked Sep 01 '18 08:09

JAVA


People also ask

Who creates the model object in Spring MVC?

After going through some tutorials and initial document reading from the docs.spring.org reference I understood that it is created in the controller of a POJO class created by the developer.

What is model object in Spring MVC?

In Spring MVC, the model works a container that contains the data of the application. Here, a data can be in any form such as objects, strings, information from the database, etc. It is required to place the Model interface in the controller part of the application.

How object is created in Spring?

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.

What is model in Spring boot MVC?

Spring Framework provides an Interface called Model(I) to work with the data. It defines a placeholder for model attributes and is primarily designed for adding attributes to the model. It is also used to transfer data between the view and controller of the Spring MVC application.


1 Answers

If not present in the model, the argument should be instantiated first and then added to the model.

The paragraph describes the following piece of code:

if (mavContainer.containsAttribute(name)) {
    attribute = mavContainer.getModel().get(name);
} else {
    // Create attribute instance
    try {
        attribute = createAttribute(name, parameter, binderFactory, webRequest);
    }
    catch (BindException ex) {
        ...
    }
}
...
mavContainer.addAllAttributes(attribute);

(taken from ModelAttributeMethodProcessor#resolveArgument)

For every request, Spring initialises a ModelAndViewContainer instance which records model and view-related decisions made by HandlerMethodArgumentResolvers and HandlerMethodReturnValueHandlers during the course of invocation of a controller method.

A newly-created ModelAndViewContainer object is initially populated with flash attributes (if any):

ModelAndViewContainer mavContainer = new ModelAndViewContainer();
mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));

It means that the argument won't be initialised if it already exists in the model.

To prove it, let's move to a practical example.

The Pet class:

public class Pet {
    private String petId;
    private String ownerId;
    private String hiddenField;

    public Pet() {
         System.out.println("A new Pet instance was created!");
    }

    // setters and toString
}

The PetController class:

@RestController
public class PetController {

    @GetMapping(value = "/internal")
    public void invokeInternal(@ModelAttribute Pet pet) {
        System.out.println(pet);
    }

    @PostMapping(value = "/owners/{ownerId}/pets/{petId}/edit")
    public RedirectView editPet(@ModelAttribute Pet pet, RedirectAttributes attributes) {
        System.out.println(pet);
        pet.setHiddenField("XXX");

        attributes.addFlashAttribute("pet", pet);
        return new RedirectView("/internal");
    }

}

Let's make a POST request to the URI /owners/123/pets/456/edit and see the results:

A new Pet instance was created!
Pet[456,123,null]
Pet[456,123,XXX]

A new Pet instance was created!

Spring created a ModelAndViewContainer and didn't find anything to fill the instance with (it's a request from a client; there weren't any redirects). Since the model is empty, Spring had to create a new Pet object by invoking the default constructor which printed the line.

Pet[456,123,null]

Once present in the model, the argument's fields should be populated from all request parameters that have matching names.

We printed the given Pet to make sure all the fields petId and ownerId had been bound correctly.

Pet[456,123,XXX]

We set hiddenField to check our theory and redirected to the method invokeInternal which also expects a @ModelAttribute. As we see, the second method received the instance (with own hidden value) which was created for the first method.

like image 123
Andrew Tobilko Avatar answered Oct 09 '22 06:10

Andrew Tobilko