Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do @SessionAttributes in SpringMVC get removed? (With code sample)

Under what exact circumstances do @SessionAttributes get cleared? I've discovered some confusing behaviour when trying to use two models in a page.

When I do a GET followed by a POST using this controller...

@Controller
@RequestMapping("/myPage*")
@SessionAttributes(value = {"object1", "object2"})
public class MyController {

  @RequestMapping(method = RequestMethod.GET)
  public String get(Model model) {
      model.addAttribute("object1", new Object1());
      model.addAttribute("object2", new Object2());
      return "myPage";
  }

  @RequestMapping(method = RequestMethod.POST)
  public String post(@ModelAttribute(value = "object1") Object1 object1) {
      //do something with object1
      return "myPage";
  }
}

...object2 gets cleared from the Model. It no longer exists as a @SessionAttribute and cannot be accessed on my view page.

However if the signature of the second method is modified to this...

public String post(@ModelAttribute(value = "object1") Object1 object1,
                   @ModelAttribute(value = "object2") Object2 object2) {

...then object2 does not get cleared from the model and is available on my view page.

The javadoc for @SessionAttributes says:

... attributes will be removed once the handler indicates completion of its conversational session.

But I don't see how I have indicated completion of the conversational session in the first example but not in the second example.

Can anyone explain this behaviour or is it a bug?

like image 631
Daniel Alexiuc Avatar asked Sep 03 '09 04:09

Daniel Alexiuc


People also ask

What is @SessionAttributes in Spring MVC?

@SessionAttribute annotation retrieve the existing attribute from the session. This annotation allows you to tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.

What is the use of @ModelAttribute in Spring MVC?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

What are session attributes?

Session attributes contain application-specific information that is passed between a bot and a client application during a session. Amazon Lex passes session attributes to all Lambda functions configured for a bot.


1 Answers

You indicate completion of the conversation by calling

SessionStatus.setComplete

public void post(...., SessionStatus status) {
  status.setComplete();
}

That said, I don't see why you should be loosing one model attribute and not the other.

Have you tried doing something like:

@ModelAttribute("object1")
public Object object1() { return new Object(); }

@ModelAttribute("object2")
public Object object2() { return new Object(); }

And see how that compares to putting the attributes in the model by hand.

like image 160
ptomli Avatar answered Oct 12 '22 03:10

ptomli