如何将复选框正确绑定到百里香中的对象列表?

如何将复选框正确绑定到百里香中的对象列表?,第1张

如何将复选框正确绑定到百里香中的对象列表?

我用自定义解决方案来解决这个问题,我实现它通过发送一个 阵列证书 ID到控制器和接收它作为 requestParam
。需求更改定义如下。

视图

         <tr>  <td>Certificate</td>  <td>    <th:block th:each="certificate : ${certificates}">     <input type="checkbox" name="cers" th:value="${certificate.id}"/>      <label th:text="${certificate.name}"></label>    </th:block>  </td></tr>

控制者

@RequestMapping(value = "/add" , method = RequestMethod.POST)public String addSave(         @ModelAttribute("employee")Employee employee ,          @RequestParam(value = "cers" , required = false) int[] cers ,         BindingResult bindingResult , Model model) {if(cers != null) {    Certificate certificate = null ;    for (int i = 0; i < cers.length; i++) {        if(certificateService.isFound(cers[i])) { certificate = new  Certificate(); certificate.setId(cers[i]);          employee.getCertificates().add(certificate);          }    }        for (int i = 0; i < employee.getCertificates().size(); i++) { System.out.println(employee.getCertificates().get(i));        }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5621863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存