0

RequestSpring Form 在调用中填充时无法成功提交 [给出绑定问题] 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 "";
 }

非常感谢您的帮助

谢谢

4

2 回答 2

0

添加到传递给方法@ModelAttribute的名为showFormtype的参数。ShowFormloadForm

@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, 
于 2013-01-09T20:52:21.147 回答
0

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

public UserEntity(){
   //
}

在此处链接到解决方案

于 2013-01-10T18:21:56.327 回答