博客源码下载:戳我一下
SpringMVC学习笔记汇总:SpringMVC学习笔记汇总
一、包装类型pojo
参数绑定
需求:
商品查询controller
方法中实现商品查询条件传入。
1、包装类型的pojo
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统的可扩展性,对原始的po进行扩展
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
2、controller
中的查询方法
@RequestMapping("/queryItems")
public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception {
//调用service查找数据库,查询商品列表,这里先使用静态的数据模拟
List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute方法
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
3、页面代码
<td>
商品的名称:<input name="itemsCustom.name" />
</td>
4、包装类型pojo
参数绑定总结
itemsCustom
和包装pojo
中的属性名一致即可。
二、数组类型的参数绑定
需求:
商品批量删除,用户在页面选择多个商品,批量删除。
实现方法:
将页面选择(多选)的商品id
,传到controller
方法的形参,方法形参使用数组接收页面请求的多个商品id
。
1、controller
中的删除方法
@RequestMapping("/deleteItems")
public String deleteItem