需求,要求批量新增或者修改一个List,在spring MVC中是不支持下面代码的写法
@RequestMapping("/test") public ModelAndView test(List<OptionVo> ov){ ModelAndView view = new ModelAndView("list"); return view; }
会抛出异常
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]:
是否其实也很简单,Spring MVC 需要支持Form表单对象的方式映射,使用get set器来填充对象。
新增一个Form
public class SettlementInfoLogForm { List<SettlementInfoLog> slList; /** * 获取slList slList */ public List<SettlementInfoLog> getSlList() { return slList; } /** * 设置slList slList */ public void setSlList(List<SettlementInfoLog> slList) { this.slList = slList; } }
再使用Form来set对象
@RequestMapping(value = "/textaaa") @ResponseBody public Object textaaa(SettlementInfo settlementInfo, SettlementInfoLogForm setForm //String[] accountPay,String[] id,String[] name ){ for (SettlementInfoLog sl : setForm.getSlList()) { System.out.println(sl.getAccountPay()); } String keySn = KeySn.getKey(); String check = checkSettlementConfirm(settlementInfo); if(null != check){ return error("参数验证失败:" + check); } settlementInfoService.settlementConfirm(keySn, settlementInfo); return null; }
前台就可以使用索引的方式对后台对象设置值了
<c:forEach items="${appList }" var="item" varStatus="index"> <input type="text" name="slList[${index.index}].accountPay" value="${workInfo.money }"/> </c:forEach>