提交期间的Spring Form绑定问题

时间:2013-01-09 20:47:19

标签: java spring spring-mvc

Request调用loadForm中填充时,弹出表单无法成功提交[给出绑定问题],但在使用setupFormObject的方法@ModelAttribute中填充时效果正常注释标记。我可以在github中提供一个简单的例子来测试是否要求:)

我花了几天时间搜索甚至使用AutoPopulatingList,但无济于事

以下示例

@ModelAttribute("showForm")
public ShowForm setupFormObject() {
    //Instantiate showForm with data
    return showForm;
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView loadForm(@RequestParam("id") String id, HttpSession session) {    
    ModelAndView modelAndView = new ModelAndView(nextPage);
    //Instantiate showForm with data
    //modelAndView.addObject("showForm", showForm);
    return modelAndView;
}

@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute("showForm") ShowForm showForm, BindingResult result, final RedirectAttributes redirectAttrs) {
     //I see changed data here in showForm when populated using @setupFormObject
     //See an exception in JSP with binding error if populated in loadForm
     return "";
 }

非常感谢您的帮助

由于

2 个答案:

答案 0 :(得分:0)

@ModelAttribute添加到showForm类型为ShowForm的参数中,该参数将传递给loadForm方法。

@RequestMapping(method = RequestMethod.GET)
public ModelAndView loadForm(@RequestParam("id") String id, 
               @ModelAttribute("showForm") ShowForm showForm, HttpSession session) {    
    ModelAndView modelAndView = new ModelAndView(nextPage);
    //Instantiate showForm with data
    //modelAndView.addObject("showForm", showForm);
    return modelAndView;
}

另外请确保您确实在执行GET请求,我怀疑这可能是一个帖子,这需要:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView loadForm(@RequestParam("id") String id, 

答案 1 :(得分:0)

问题在于构造函数。它是一个私人或缺失的公共构造函数。通过添加公共构造函数,Spring能够重新创建对象

public UserEntity(){
   //
}

链接到解决方案here