我正在使用Spring并向控制器发送复选框值。它工作正常,直到我添加了enctype =" multipart / form-data"至 。现在,当我取消选中复选框时,我收到一条错误说"映射到复选框的字节值不能为空"。我错过了什么?
JSP:
<form:form modelAttribute="ad" method="post" class="form-horizontal" autocomplete="off" id="add-ad-form"
action="?${_csrf.parameterName}=${_csrf.token}" enctype="multipart/form-data">
<div class="form-group">
<label for="adContact.phoneText" class="col-sm-2 control-label">Text Me</label>
<div class="col-sm-5">
<form:checkbox path="adContact.phoneText" value="1" />
</div>
</div>
Spring生成html为:
<input id="adContact.notifyMe1" name="adContact.notifyMe" value="1" checked="checked" type="checkbox">
<input name="_adContact.notifyMe" value="on" type="hidden">
控制器:
@RequestMapping(value = "/ad/adAdd3/{categoryId}", method = RequestMethod.POST)
public String postAdAdd(@RequestParam("categoryId") int categoryID,
@ModelAttribute("ad") @Valid Ad ad, BindingResult aaResult,
RedirectAttributes attr, SessionStatus aaStatus,
Principal aaPrincipal) {
if (aaResult.hasErrors()) {
attr.addFlashAttribute("org.springframework.validation.BindingResult.ad", aaResult);
attr.addFlashAttribute("ad", ad);
return "redirect:/ad/adAdd3/" + categoryID;
} else {
ad = this.caService.saveAd(ad, categoryID,
((UserDetailsImpl) ((Authentication) aaPrincipal)
.getPrincipal()).getUser());
aaStatus.setComplete();
return "redirect:/ad/adDetail/" + ad.getId();
}
}
答案 0 :(得分:1)
添加此代码解决了我的问题。