需求:批量修改商品信息
一.显示所有商品信息
1.controller层开发
//查询商品列表(批量修改)
@RequestMapping("/displayItems")
public ModelAndView displayItems() throws Exception{
List<Items> itemsList = itemsService.getItemsList();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList",itemsList);
modelAndView.setViewName("items/displayItems");
log.info("itemList:"+itemsList);
return modelAndView;
}
2.service层、dao层的开发与案例3(ssm整合)相同。
3.web层开发
<c:forEach items="${itemsList}" var="item" varStatus="status">
<tr>
<td>${item.itemId}</td>
<td><input type="hidden" name="itemsCustomList[${status.index}].itemId" value="${item.itemId}"/>
<input type="text" name="itemsCustomList[${status.index}].itemName" value="${item.itemName}"/></td>
<td><input type="text" name="itemsCustomList[${status.index}].itemPrice" value="${item.itemPrice}"/></td>
<td><input type="text" name="itemsCustomList[${status.index}].itemDetail" value="${item.itemDetail}"/></td>
<td><input type="text" name="itemsCustomList[${status.index}].itemCreateDate" value="<fmt:formatDate value="${item.itemCreateDate}" pattern="yyyy-MM-dd hh:mm:ss"/>"/></td>
</tr>
</c:forEach>
二.批量修改数据库的商品信息
1.controller层开发
//查询商品列表(批量修改)
@RequestMapping("/updateItems")
public ModelAndView updateItems(ItemsQueryVo itemsQueryVo) throws Exception{
ModelAndView modelAndView = new ModelAndView();
try {
for (int i = 0; i < itemsQueryVo.getItemsCustomList().size(); i++) {
itemsService.updateItems(itemsQueryVo.getItemsCustomList().get(i).getItemId(), itemsQueryVo.getItemsCustomList().get(i));
}
}catch (Exception e){
e.printStackTrace();
}
modelAndView.setViewName("items/success");
return modelAndView;
}
2.service层、dao层、web层的开发与案例4参数绑定(基本数据类型和pojo类型)相同。