Spring MVC - @SessionAttributes with @ModelAttribute MCQ
0/13
0✓
0✗
1. At which level is the @SessionAttributes annotation used?
Why:@SessionAttributes is applied on a class, typically a @Controller class.
2. What does the value element of @SessionAttributes contain?
Why: The value is a String[] of model attribute names. These names match the names used in @ModelAttribute.
3. What is the main purpose of @SessionAttributes?
Why:@SessionAttributes stores the named model attributes in the HttpSession so they can be reused in later requests of the same session.
4. A controller has @SessionAttributes("user") and a method annotated with @ModelAttribute("user"). When is this @ModelAttribute method called?
Why: Spring first looks for the attribute in the session. Only if it is not found, the @ModelAttribute method is invoked.
5. What happens to the value returned by a @ModelAttribute method when its name is listed in @SessionAttributes?
Why: The returned value is used to populate the HttpSession with the same attribute name.
6. In the same HTTP session, an attribute is already stored by @SessionAttributes. What does Spring do on the next requests?
Why: Within the same session, Spring gets the same object from the session. The @ModelAttribute method is not called again until the attribute is removed or the session ends.
7. A handler method parameter is annotated with @ModelAttribute("user"), and the name is listed in @SessionAttributes. Where does Spring get the value for this parameter first?
Why: Spring retrieves the value from the session and passes it to the handler method parameter.
8. Which exception is thrown when a handler needs a session attribute through @ModelAttribute, but it is not in the session and no @ModelAttribute method can create it?
Why: Spring throws HttpSessionRequiredException with a message saying that a session attribute was expected.
9. How long does a session attribute stay in the HttpSession after being added through @SessionAttributes?
Why: The attribute stays in the session until the session ends. It can also be removed earlier by completing the session processing.
10. Which type can be used as a handler method parameter to remove @SessionAttributes attributes from the session?
Why:org.springframework.web.bind.support.SessionStatus can be added as a handler method parameter to control the session attribute lifecycle.
11. Which method of SessionStatus is called to signal that session processing is complete?
Why: Calling setComplete() tells Spring that session processing is done. Spring then removes the session attributes declared by @SessionAttributes.
12. What happens after SessionStatus.setComplete() is called?
Why: Spring removes only the attributes declared in @SessionAttributes using HttpSession.removeAttribute(). The session itself stays alive.
13. A session attribute was removed by calling setComplete(). A later request in the same session needs that attribute again. What happens?
Why: Since the attribute is no longer in the session, Spring calls the @ModelAttribute method again and stores the new value in the session.
14. Which of these is a good use case for @SessionAttributes?
Why:@SessionAttributes is useful when data must be kept between multiple requests of the same user session, such as visited pages or a shopping cart.
15. What do the names in @SessionAttributes need to match?
Why: The values in @SessionAttributes must match the names of the model attributes used by @ModelAttribute, on a method or on a handler method parameter.