views:

18

answers:

1

I have a domain object with 5 property. I preload the object in my GET method and display just one of the property in the form. When the form gets submitted, the object contains only one property with value. How do I get the remaining properties and their values without putting a hidden variable for each property in my form.

+1  A: 

If you don't want to store properties in hidden fields, you can store your object in the session. In Spring 3 this can be done declaratively with @SessionAttribute annotation:

@Controller @RequestMapping("/editBar")
// Specifiy the name of the model attribute to be stored in the session
@SessionAttribute("bar")     
public class BarController {

    @RequestMapping(method = GET) 
    public String form(Map<String, Object> model) {
        model.put("bar", ...);
        ...
    }

    @RequestMapping(method = POST)
    public String submit(@ModelAttribute("bar") Bar bar, BindingResult errors, 
        SessionStatus status) {
        if (!errors.hasErrors()) {
            status.setComplete(); // Clear the session after successful submit
            ...
        } ...
    }
}
axtavt