我正在加载用户对象,然后将其作为命令对象存储在控制器中的GET模型中。此用户对象具有许多未在jsp页面中映射的属性。提交表单后,我在POST上获取控制器上的命令对象。但奇怪的是,我只能看到命令对象中映射到jsp页面的属性。我加载对象时出现的所有其他属性均丢失。我需要对象中的所有属性能够成功地将它保存在数据库中。Spring MVC基于Annotation的Controller控制器命令对象在POST上丢失值
任何人都可以帮我解决这个问题吗?谢谢!
更新
我加入一些代码,以便更好地理解它。在POST处理程序中,我期待命令对象除了使用jsp绑定的属性外,还具有GET处理程序中加载的所有属性。相反,我失去了除jsp之外的所有propeties。我在这里做错了什么?
@RequestMapping(method = RequestMethod.GET)
public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception {
UserBean user = Util.getUser(session);
UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName());
model.addAttribute("command", command);
return formView;
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception {
UserBean user = (UserBean) command;
userProfileService.saveUser(user);
return "successView";
}
更新
我加入一些代码,以便更好地理解它。在POST处理程序中,我期待命令对象除了使用jsp绑定的属性外,还具有GET处理程序中加载的所有属性。相反,我失去了除jsp之外的所有propeties。我在这里做错了什么?
@RequestMapping(method = RequestMethod.GET) public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception { UserBean user = Util.getUser(session); UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName()); model.addAttribute("command", command); return formView; }
@RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception { UserBean user = (UserBean) command; userProfileService.saveUser(user); return "successView"; }
更新
如果我保存命令对象会如何将jsp的绑定化子性质。我以为我需要将它存储在模型中?请问你能解释一下吗?
更新
存储会话的命令对象解决了这个问题。我可以通过使用
@SessionAttributes ("command")
非常感谢!
添加信息时不要添加一个答案。你应该更新你的问题。我这次是为你做的(你现在应该删除'答案')。 – Bozho 2010-12-07 16:56:01