1、数组
需求:服务端接受浏览器传来的CheckBox参数:
页面:写一个表单,表单中只有CheckBox表单
<form action="${pageContext.request.contextPath }/getIds.action" method="post">
<c:forEach items="${itemList }" var="item">
<tr>
<td><input type="checkbox" name="ids" value="${item.id }" /></td>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
<input type="submit" value="提交"/>
</form>
controller:用数组接受前端传来的CheckBox的数组值
@RequestMapping(value="/getIds.action")
public String getIds(Integer[] ids) {
System.out.println(ids);
return "success";
}
结果:
2、list集合
list只能放到包装类中,包装类作为形参,list不能直接作为形参
包装类:在包装类中写一个list属性
package club.ityuchao.pojo;
import java.util.List;
public class QueryVo {
private Items item;
private List<Items> items;
public List<Items> getItems() {
return items;
}
public void setItems(List<Items> items) {
this.items = items;
}
public Items getItem() {
return item;
}
public void setItem(Items item) {
this.item = item;
}
}
页面:页面name值为:items[0].id、items[0].name
<form action="${pageContext.request.contextPath }/updateItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="修改"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="text" name="items[${s.index }].name" value="${item.name }" /></td>
<td><input type="text" name="items[${s.index }].price" value="${item.price }" /></td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
controller:在controller方法中写上包装类的形参
@RequestMapping(value="/updateItems.action")
public String updateItems(QueryVo queryVo) {
ItemService.updateItems(queryVo);
return "success";
}
接受结果: