前端传递集合:
<tr>
<td>
<select name="indtablelist[0].commodity_id">
<c:forEach items="${commoditys}" var="comm">
<option value="${comm.id}">${comm.commodity_no}||${comm.commodity_name}</option></c:forEach>
</select>
</td>
<td><input type="text" name="indtablelist[0].quantity"/></td>
<td><input type="text" name="indtablelist[0].memo"/></td>
</tr>
<tr>
<td>
<select name="indtablelist[1].commodity_id">
<c:forEach items="${commoditys}" var="comm">
<option value="${comm.id}">${comm.commodity_no}||${comm.commodity_name}</option></c:forEach>
</select>
</td>
<td><input type="text" name="indtablelist[1].quantity"/></td>
<td><input type="text" name="indtablelist[1].memo"/></td>
</tr>
后台接收集合:
spring mvc 不能直接接收数组list参数, 必需把这个数组或list作为一个对象的属性接收
public class IndtableList {
List<Indtable> indtablelist = new ArrayList<Indtable>();
public List<Indtable> getIndtablelist() {
return indtablelist;
}
public void setIndtablelist(List<Indtable> indtablelist) {
this.indtablelist = indtablelist;
}
}
Controller接收集合:
@RequestMapping("/addPlan.do")
public String addPlan(IndtableList indtablelist){
//生成唯一的订单号
int i = indentmapper.allrows()+1;
String now=new SimpleDateFormat("yyyyMMdd-").format(new Date(System.currentTimeMillis()));
//生成订单
Indent indent = new Indent();
indent.setIndent_no("OR"+now+i);
indentmapper.add(indent);
List<Indent> indents = indentmapper.findAll();
int indent_id = 0;
for(Indent ind:indents){
if(ind.getId()>indent_id)
indent_id = ind.getId();
}
List<Indtable> inds = indtablelist.getIndtablelist();
for(Indtable ind:inds){
ind.setIndent_id(indent_id);
indtablemapper.add(ind);
}
return "redirect:findAll.do";
}