【FAQ】SpingMVC实现集合参数(Could not instantiate bean class [java.util.List])

需求,要求批量新增或者修改一个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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_72429728

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值