Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SessionAttributes to be exposed in model even when not referenced in current handler method [SPR-6084] #10752

Closed
spring-projects-issues opened this issue Sep 6, 2009 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Sep 6, 2009

Daniel Alexiuc opened SPR-6084 and commented

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.

This behaviour seems like a bug - I believe that object2 should remain in the model until I have indicated that the conversational session is complete.


Affects: 2.5.6

Reference URL: http://forum.springsource.org/showthread.php?t=77305

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This seems to be a problem with Spring's model exposure rules: We only expose attributes that are actually added or referenced in the current handler method. Session attributes would stay around in the session even when not referenced but wouldn't get exposed in the model... which is what you seem to be experiencing.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

As of 3.0 RC1, we expose all @SessionAttributes to the model - independent from whether they have been referenced in the handler method signature.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Pat Leamon commented

This leads to some weird behaviour on redirects - session attributes get exposed on the url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants