开发者

spring mvc annotation - object missing values on post

开发者 https://www.devze.com 2023-01-12 02:17 出处:网络
I have a domain object with 5 property. I preload the object in my GET method and display just开发者_如何学C one of the property in the form. When the form gets submitted, the object contains only one

I have a domain object with 5 property. I preload the object in my GET method and display just开发者_如何学C 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.


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
            ...
        } ...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号