Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC3.1.2 @Controller losing command values on POST if not shown in JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC3.1.2 @Controller losing command values on POST if not shown in JSP

    Using Spring MVC3.1.2 @Controller annotation. Getting user object with GET method.
    But after POST, if any if user field is not displayed in jsp/view , the value is lost.
    Do not want to display certain fields to be edited, so not showing in edit page.

    Do not want to use @SessionAttributes("user") as mentioned in some forums.
    Do not want to use hidden variables. Too many fields. Any solution other than creating different beans?

    Creating different beans is not option as some variable are displayed one forms and others in few forms.

    Why is this feature taken out in spring 3.1.2 ? is there any solution? or I am missing something?

    This was not the case in Springformcontroller in spring2.5. Object values form object were retained in the object if displayed or not. Why this not working in spring 3.1.2 @Controller annotation? Should revert back to spring2.5?


    Here is the code snippet



    @Controller
    @RequestMapping("/users")
    public class UserController {
    //autowired service

    @RequestMapping(value="/edit", method=RequestMethod.GET)
    public String getEdit( Model model, @RequestParam("id") Long id) {
    User user = userService.getUser(id);
    if (user != null) {
    model.addAttribute("user",user);
    }
    return "userForm";
    }

    @RequestMapping(value="/edit", method=RequestMethod.POST)
    public String editUser(@ModelAttribute("user") User user , BindingResult result, @RequestParam("id") Long id) {
    try {
    userService.updateUser(user);
    } catch (Exception ex) {
    result.reject(ex.getMessage()) ;
    return "userForm";
    }
    return "redirect:users";
    }

    ...
    //other methods
    }

    public class User implements java.io.Serializable, UserDetails {
    private Long id;
    private String username;
    private String password;
    private String email;
    ... loads of other fields with getter and setter
    Thanks. Any help /solution is appreciated.

  • #2
    Why is this feature taken out in spring 3.1.2 ? is there any solution? or I am missing something?
    It isn't taken out it is just that it works different in the SimpleFormController there was a predefined strict workflow the @Controllers are more flexible. First understand the workflow in the SimpleFormController then do this in your @Controller.

    This was not the case in Springformcontroller in spring2.5. Object values form object were retained in the object if displayed or not. Why this not working in spring 3.1.2 @Controller annotation? Should revert back to spring2.5?
    This was only because of the predefined strict workflow (as mentioned above) for each request the formBackingObject method was called unless you stored something in the session. In your @Controller there is NO replacement for the formBackingObject method (if you don't want to store it in the session in between requests)...

    So simply add a replacement (and no the initial GET method is not the replacement). Simply add a method annotated with @ModelAttribute which finds the user this method is executed before each request handling method (as explained in the reference guide which I strongly suggest as a must read). Which mimics the behavior of the formBackingObject method in the old SimpleFormController.

    Comment

    Working...
    X