springmvc中的对象、数组、集合类型的参数绑定

来源:互联网 发布:国际网络电话软件排名 编辑:程序博客网 时间:2024/05/20 09:21

一、pojo类型的参数绑定

实现方式:

1、在controller方法的形参中添加HttpServletRequest对象参数,通过request接收查询条件参数

<span style="white-space:pre"></span>@RequestMapping("/queryItems")public ModelAndView queryItems<span style="background-color: rgb(255, 255, 102);">(HttpServletRequest request)</span> throws Exception {request.getParameter("参数名");<span style="white-space:pre"></span>//。。。。}
2、controller方法的形参中让包装的pojo接收查询条件参数,页面中使用包装的pojo中的属性.属性名的方式进行传参,在controller形参中,直接添加包装的pojo变量即可。

controller方法的定义

@Controller@RequestMapping("/items")public class ItermsController {@Autowiredprivate ItemsService itemsService;@RequestMapping("/queryItems")public ModelAndView queryItems(<span style="background-color: rgb(255, 255, 102);">QueryItemsCustomVo queryItemsCustomVo</span>) throws Exception {// 调用service查找数据库,查询商品列表,List<ItemsCustom> itemList = itemsService.findItemsList(queryItemsCustomVo);// 返回ModelAndViewModelAndView modelAndView = new ModelAndView();// 相当于request的setAttribute,在jsp页面中通过itemList获取数据modelAndView.addObject("itemList", itemList);// 指定视图// 下边的路径,如果在视图解析器中配置了jsp路径的前缀和后缀,则可以修改为// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");modelAndView.setViewName("/items/itemsList");// 上面的路径配置,可以不再程序中指定jsp地址的前缀和后缀return modelAndView;}}
页面的定义:注意itemsCustom和包装类QueryItemsCustomVo中的属性一致

<span style="white-space:pre"></span><table width="100%;" border="1"><tr><td>商品名称:<input type="text"<span style="background-color: rgb(255, 255, 102);"> name="itemsCustom.name"</span> /></td></tr><tr><td><input type="button" value="查询" onclick="queryItems();"/><input type="button" value="批量删除" onclick="deleteItems();"/></td></tr></table>
QueryItemsCustomVo.java

package com.sky.ssm.po;import java.util.List;/** * 商品查询类 作用:为商品的查询提供相应的查询条件 */public class QueryItemsCustomVo {private Items items;private ItemsCustom <span style="background-color: rgb(255, 255, 102);">itemsCustom</span>;//批量商品信息private List<ItemsCustom> <span style="background-color: rgb(255, 204, 204);">itemsList</span>;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;}public List<ItemsCustom> getItemsList() {return itemsList;}public void setItemsList(List<ItemsCustom> itemsList) {this.itemsList = itemsList;}}

二、数组类型的参数绑定

controller方法的定义:

@RequestMapping("/deleteItems")public String deleteItems(<span style="background-color: rgb(255, 255, 0);">Integer[] items_id</span>) throws Exception{//调用service批量删除商品return "success";}

页面的定义:

<table width="100%;" border="1"><tr><td>选择</td><td>商品名称</td><td>商品价格</td><td>生产日期</td><td>商品描述</td><td>操作</td></tr><c:forEach items="${itemList }" var="item"><tr><td><input type="checkbox" name="<span style="background-color: rgb(255, 255, 0);">items_id</span>" 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 }/items/editItems.action?id=${item.id}">修改</a></td></tr></c:forEach></table>
注意的是:页面参数的名称要和controller方法中形参的名称一致,这样才能进行正确的参数绑定

三、List集合类型的参数绑定

controller方法的定义:

@RequestMapping("/editItemsAllSubmit")public String editItemsAllSubmit(<span style="background-color: rgb(255, 255, 0);">QueryItemsCustomVo queryItemsCustomVo</span>) throws Exception{return "success";}
页面的定义:

<table width="100%;" border="1"><tr><td>商品名称</td><td>商品价格</td><td>生产日期</td><td>商品描述</td></tr><c:forEach items="${itemList }" var="item" varStatus="status"><tr><td><!-- itemsList对应controller方法的形参中pojo对象中的itemsList属性;itemsList[${status.index }]对应于itemsList中第${status.index }个对象,id为第${status.index }个对象的属性 --><input type="hidden" name="itemsList[${status.index }].id" value="${item.id}"/><input name="itemsList[${status.index }].name" value="${item.name }"/></td><td><input name="<span style="background-color: rgb(255, 204, 204);">itemsList[${status.index }]</span>.price" value="${item.price }"/></td><td><input name="itemsList[${status.index }].createtime" value="<fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss" />"/></td><td><input name="itemsList[${status.index }].detail" value="${item.detail }"/></td></tr></c:forEach></table>
注:在controller方法的形参对象queryItemsCustomVo中定义了list集合类型的属性(看前面的queryItemsCustomVo的定义),页面中List类型的参数,需要通过包装类来进行接收,页面中的参数名称要与包装类中的属性名称一致。

四、Map集合类型的参数绑定

通过在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。包装类中定义的Map对象如下:

public class QueryVo{private Map<String,Object><span style="background-color: rgb(255, 255, 102);"> itemInfo</span> = new HashMap<String, Object>();//get/set方法}
页面的定义如下:

<tr><td>学生信息:</td><td>姓名:<input type=”text” name=”<span style="background-color: rgb(255, 255, 51);">itemInfo[‘</span><span style="background-color: rgb(255, 204, 204);">name</span><span style="background-color: rgb(255, 255, 51);">’]</span>”/></td></tr>
controller方法的定义如下:

Public String useraddSubmit(QueryVo queryVo){}

同样的道理,页面中的参数名称对应于包装对象中的属性名称,页面参数中的name 就是map集合的key,输入的值,就是map集合的value







0 0
原创粉丝点击