我通过的布尔值集合的形式被复选框的每个元素修改。当属性(集合)发送到控制器时,返回为null。
该集合称为isSubstituted,并且我使用另一个称为items的列表,在其中以表格形式打印替换或不替换的项目的名称。
控制器的代码为:
@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
public ModelAndView save(@Valid final Revision revision, @RequestParam Collection<Boolean> isSubstituted, final BindingResult binding) {
ModelAndView result = new ModelAndView();
Revision saved;
Collection<Boolean> res = isSubstituted;
if (binding.hasErrors())
result = this.createEditModelAndView(revision);
else
try {
saved = this.revisionService.save(revision);
Collection<Revision> revisions = this.revisionService.getRevisionsByVehicle(saved.getVehicle().getId());
result.addObject("revisions",revisions);
result = new ModelAndView("redirect:/revision/garage/listByVehicle?varId=" + saved.getVehicle().getId());
} catch (final Throwable oops) {
result = this.createEditModelAndView(revision, "commit.error");
}
return result;
}
另一种方法是:
protected ModelAndView createEditModelAndView(final Revision revision, final String messageCode) {
ModelAndView result;
Vehicle vehicle = revision.getVehicle();
Collection<String> items = new ArrayList<>();
Configuration c = this.configurationService.findAll().iterator().next();
Collection<Boolean> isSubstituted = revision.getIsSubstituted();
if(revision.getVehicle().getType().equals(VehicleType.motorbike))
items = c.getItemsForMoto();
else
items = c.getItemsForCar();
result = new ModelAndView("revision/edit");
result.addObject("vehicle", vehicle);
result.addObject("revision", revision);
result.addObject("items", items);
result.addObject("isSubstituted", isSubstituted);
result.addObject("message", messageCode);
result.addObject("requestURI", "/revision/garage/edit");
return result;
}
该视图的代码为:
<jstl:forEach var="item" items="${items}" varStatus="count">
<p><jstl:out value="${item}"/>:</p>
<p>
<label/>
<input path="${isSubstituted[count.index]}" name="${item}" type="checkbox"/>
<span><jstl:out value="Substituted"/></span>
</label>
</p>
</jstl:forEach>
实际结果是该属性(名为isSubstituted的列表)为空。