spring-springmvc-mybatis整合笔记(8)——集合类型参数绑定

一 数组

1 需求

商品批量删除,用户在页面选择多个商品,批量删除。

2 表现层实现

关键:将页面多选的商品ID,传到controller方法的形参中,方法形参使用数组接受传过来的ID。

JSP页面修改后为:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
    <script type="text/javascript">
        function deleteItems() {
            document.itemsForm.action="${pageContext.request.contextPath}/items/deleteItems.action";
            document.itemsForm.submit();
        }
        function queryItems() {
            document.itemsForm.action="${pageContext.request.contextPath }/items/queryItem.action";
            document.itemsForm.submit();
        }
    </script>
</head>
<body> 
<form name="itemsForm" action="${pageContext.request.contextPath }/items/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询" onclick="queryItems()"/>
<input type="button" value="批量删除" onclick="deleteItems()"></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>选择</td>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
    <td<input type="checkbox" name="items_id" 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>
</form>
</body>

</html>

Controller中新增方法

// 批量删除 商品信息
@RequestMapping("/deleteItems")
public String deleteItems(Integer[] items_id) throws Exception

二 List

1 需求

通常在需要批量提交数据时,将提交的数据绑定到list<pojo>中,比如:成绩录入(录入多门课成绩,批量提交),

本例子需求:批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。

2 表现层实现

controller方法定义:

1 进入商品批量修改页面

2 提交

包装类型中新增属性

public class ItemsQueryVo {

    //商品信息
    private Items items;

    //为了系统 可扩展性,对原始生成的po进行扩展
    private ItemsCustom itemsCustom;

    //批量商品信息
    private List<ItemsCustom> itemsList;

Controller方法中

// 批量修改商品提交
// 通过ItemsQueryVo接收批量提交的商品信息,将商品信息存储到itemsQueryVo中itemsList属性中。
@RequestMapping("/editItemsAllSubmit")
public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception {

    return "success";
}

@RequestMapping("/editItemsQuery")
    public ModelAndView editItemsQuery(HttpServletRequest request,ItemsQueryVo itemsQueryVo) throws Exception {
        List<ItemsCustom> itemsCustomList = itemsService.findItemsList(null);

        ModelAndView modelAndView = new ModelAndView();

        //与request.setAttribute方法功能一样
        modelAndView.addObject("itemsList", itemsCustomList);
        //以下路径如果已在视图解析器中配置JSP的前后缀,修改为items/itemsList
        //modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        modelAndView.setViewName("editItemsQuery");

        return modelAndView;
    }

在JSP页面中新增editItemsQuery.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
    <script type="text/javascript">
        function editItemsAllSubmit() {
            document.itemsForm.action="${pageContext.request.contextPath}/items/editItemsAllSubmit.action";
            document.itemsForm.submit();
        }
        function queryItems() {
            document.itemsForm.action="${pageContext.request.contextPath }/items/queryItem.action";
            document.itemsForm.submit();
        }
    </script>
</head>
<body> 
<form name="itemsForm" action="${pageContext.request.contextPath }/items/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
    商品名称<td><input name="itemsCustom.name"></td>
<td><input type="submit" value="查询" onclick="queryItems()"/>
<input type="button" value="批量修改提交" onclick="editItemsAllSubmit()"></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
    <<td><input name="itemsList[${status.index }].name" value="${item.name }"/></td>
	<td><input name="itemsList[${status.index }].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>
</form>
</body>

</html>

name的格式:

对应包装pojo中的list类型属性名[下标(从0开始)].包装pojo中List类型的属性中pojo的属性名

例子:

"name="itemsList[${status.index }].price"

可以和包装类型的参数绑定归纳对比一下,其实就是在包装类的pojo基础上多了个下标。只不过包装类参数绑定时,要和包装pojo中的pojo类性的属性名一致,而list参数绑定时,要和包装pojo中的list类型的属性名一致。

三 Map

在包装类中定义下Map属性(并完成get set方法)

public class QueryVo {
private Map<String, Object> itemInfo = new HashMap<String, Object>();
  //get/set方法..
}

页面定义如下

<tr>
<td>学生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年龄:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>

猜你喜欢

转载自blog.csdn.net/lpckr94/article/details/80957591