-
Notifications
You must be signed in to change notification settings - Fork 38.4k
implicit creation of model (session) attribute [SPR-4408] #9086
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
Comments
Marten Deinum commented Wouldn't this also be achieved by the following
|
Andreas Schildbach commented If I understand the mechanism correctly, your |
Bartek commented I've got the same problem (discussed deeply on forum http://forum.springsource.org/showthread.php?p=211420) Shortly speaking I would like to implement the following scenario (all requests go to the same url ):
I've tried different solutions:
In my opinion the in other words,. method:
should not be executed if attribute "command" is already available in session. |
Jeremy Haile commented We are using Spring 2.5.1 and experiencing this problem. I won't expand too much since Bartek summarized our problem exactly in his last comment. One of our usages is that we want to have a GET method whose parameters are bound to a command object, but the command is stored in the session so that later GETs will retain the previous settings (although params would overwrite them) For example: public void browseUsers( This does not work at all now for the reasons described by Bartek. I will also mention that even though the solution he described (not executing a Why should I have to write: when I could just write: and let Spring create the command, bind to it, and store it in the session for me? |
Phil Krasko commented Is there any workaround for this? I have a bunch of pages using a large DTO that I would like to store as a This functionality would definitely be useful! |
Alex Savitsky commented The only workaround so far is to replace the method resolveModelAttribute in class org.springframework.web.bind.annotation.support.HandlerMethodInvoker, to create a new attribute if one's not found in session. Specifically, replace this
with this
|
Alex Savitsky commented yes, and I'm using Spring 3, it haven't been fixed there, either. I'd even classify this as a bug fix, not improvement, as this behavior makes much more sense than the original one. |
Phil Krasko commented Yes, that's a viable way to fix it. -spring 2.5.6 |
Alex Savitsky commented Could you please share the interceptor code and config? I'd rather leave the core classes alone, too, just didn't know of that possibility. |
Phil Krasko commented Alex, I actually tried to tackle 2 issues in what I created. Issue #1 is described above in this ticket. The interceptor I have accepts a new annotation I created called " Example:
} A quick explanation of what happens: Let me know if you're interested and maybe you can send me you email address. I really would like another eye to go through the code. I created in in one day and only tested it on 2 controllers or so. |
Tamas Perlaky commented Assuming that your session attribute store is actually the session (which I guess might not always be true?), you could check the session to see if it's already there before instantiating a new object:
I know the initial assumption may not be valid, but how dangerous would this really be in practice? |
Rossen Stoyanchev commented This ticket has accumulated quite a few comments. I will try to summarize and see if there is anything unresolved. The original description was to fall back on the no-arg constructor for
That should work as well using a solution similar to this: @SessionAttributes(types=Command.class)
@Controller
public class CommandController {
@ModelAttribute
public Command createCommand(@PathVariable long id) {
// retrieve command ...
return command;
}
@RequestMapping(value="/{id}/edit", method=GET)
public void edit() {
}
@RequestMapping(value="/{id}", method=PUT)
public String update(@ModelAttribute Command command) {
// do something with command ...
return "edit";
}
} With that in mind is there anything that remains unresolved? |
Rossen Stoyanchev commented Closing this issue for now without any further comments or use cases to consider. |
Andreas Schildbach opened SPR-4408 and commented
If you declare a model attribute with
@ModelAttribute
in a handler method (parameter annotation), it would be convenient if the framework would construct an empty attribute for you, if there isn't one already and there is a no-arg constructor on the attribute's class.Instead of code like this
@RequestMapping
public String setupForm(ModelMap model) {
Command command = new Command();
command.setXyz(defaultValue);
model.addAttribute("command", command);
return view;
}
you could just write
@RequestMapping
public String setupForm(
@ModelAttribute
Command command) {command.setXyz(defaultValue);
return view;
}
This feature would of course apply to
@SessionAttributes
, too.If this feature looks too magical, you could also introduce a boolean parameter on the
@ModelAttribute
annotation, saying whether implicit creation is allowed or not.Affects: 2.5.1
9 votes, 10 watchers
The text was updated successfully, but these errors were encountered: