I have tried and tried but can't figure out what is going on here.
- I have a simple controller annotated using @Controller
- I also have annotation for @SessionAttributes
- I handle a GET request and put an object into the model.
- When I get back the POST from the form, I only get back what the user has populated. I'm not getting back the complete object.
I'm new to SessionAttributes but I thought this preserved the whole object and when the object was read back in the method using @ModelAttribute, it would be merged the object (i.e. the object that the form changed). However, I'm not seeing this behavior.
Any help would be much appreciated.
Here is the relevant pieces from the code:
@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController
{
// ...
@RequestMapping(value = "/{login}", method = RequestMethod.GET)
public String profile(Model model, @PathVariable("login") String login)
{
// ...
model.addAttribute("user", user);
// ...
}
@RequestMapping(value="/{login}", method = RequestMethod.POST)
public String saveProfile(@ModelAttribute("user") @Valid User user, BindingResult result, SessionStatus status)
{
if (result.hasErrors())
{
return "user/index";
}
// ...
status.setComplete();
return "redirect:/user/"+user.getLogin();
}
Do you see anything that I may have missed? I have spent almost a day trying to figure this out and just can't. Any help would be much appreciated.
Update: I figured out what the issue was. Answer posted below.