1. What does @ModelAttribute do when it is placed on a handler method parameter?
Why: At the parameter level, @ModelAttribute means the value should be retrieved from the Model and passed to the handler method.
2. What does @ModelAttribute do when it is placed on a method (method level)?
Why: A method annotated with @ModelAttributepopulates the Model before the request handler method is invoked.
3. When does Spring call the methods annotated with @ModelAttribute?
Why: Spring calls all @ModelAttribute methods before invoking the handler method that matches the request.
4. Which pair of steps lets a handler method receive a value that was prepared in the Model?
Why: First, a @ModelAttribute method returns an object and it is stored in the Model. Second, a handler parameter annotated with @ModelAttribute and the same name receives that object.
5. How does Spring find the object for a handler parameter annotated with @ModelAttribute("item")?
Why: Spring uses the name given in @ModelAttribute to look up the object in the Model map.
6. Which annotation tells Spring to get the value of the user parameter from the Model?
@RequestMapping("profile")
public String show (/* annotation here */ User user) {
...
}
Why:@ModelAttribute("user") on a parameter tells Spring to take the value from the Model using the name user.
7. What happens if Spring cannot find a matching named object in the Model for a @ModelAttribute parameter?
Why: If no match is found, Spring tries to create the object using its default constructor, assigns it to the parameter, and then calls the handler.
8. Where does Spring put the return value of a method annotated with @ModelAttribute?
Why: All return values of @ModelAttribute methods are added to the Model object.
9. An object is already in the Model and the view can use it. Why would we still add it as a @ModelAttribute parameter to a handler method?
Why: The object is already in the Model for the view. We add it as a parameter when we want to use it in the handler method, for example to choose a view name based on one of its values.
10. What can be the return type of a method annotated with @ModelAttribute?
Why: A @ModelAttribute method can return an object of any type. That object is then added to the Model. it can also have a void return type when it accepts a Model (or Map) parameter and adds attributes directly inside the method body.
11. A controller class has the @SessionAttributes annotation. What does Spring check first for a @ModelAttribute handler parameter?
Why: When @SessionAttributes is used, Spring first checks if the named value is in the HttpSession.
12. A controller uses @SessionAttributes and the named value is found in the HttpSession. What does Spring do with the related @ModelAttribute method?
Why: If the value is found in the session, Spring uses it instead of invoking the related @ModelAttribute method.
13. Which use of @ModelAttribute adds attributes to the Model before the handler method is invoked?
Why: The method-level use populates the Model before the handler runs. The parameter-level use retrieves an object from the Model for the handler.