为什么我的@ModelAttribute id 没有保留?为什么我必须将其作为隐藏参数发送到我的表单中?使用命令时不应该弹簧处理这个吗?
@RequestMapping(value="/{supplierId}", method=RequestMethod.GET)
public String get(@PathVariable Long supplierId, Model model, Principal principal){
Form form = .... //Got a an existing form from DB
model.addAttribute("form", form);
return "/form";
}
@RequestMapping(value="/{supplierId}", method=RequestMethod.POST)
public String post(HttpServletRequest request, @PathVariable Long supplierId, @Valid @ModelAttribute("form") Form form, BindingResult result, Model model, Principal principal){
System.out.println(form.getID()); //Here the id is 0
safeFoodFormService.store(form, supplierId);
return "redirect:/supplier";
}
我的 Freemarker 表格
<form class="form-horizontal" action="<@spring.url "/forms/${supplier.ID?c}"/>" method="post" command="form">
如果我在表单中添加此行,它将起作用
<@spring.bind "form.ID" />
<input type="hidden" name="${spring.status.expression}" value="${spring.status.value default("")}" />
如果这可以由 Spring 处理,我会非常好。谢谢