2010-08-19 19 views
0

我有一個具有5個屬性的域對象。我在我的GET方法中預載對象,並只顯示錶單中的一個屬性。當表單被提交時,對象只包含一個帶有值的屬性。如何獲取剩餘屬性及其值,而無需爲表單中的每個屬性添加隱藏變量。spring mvc註解 - 對象缺失值在崗位

回答

3

如果您不想在hidden字段中存儲屬性,則可以將對象存儲在會話中。在春季3,這可以通過@SessionAttribute註釋以聲明方式完成:

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