您的位置:首页 > 编程语言 > Java开发

SpringMVC学习笔记(十)——包装类型pojo、数组、集合的参数绑定

2018-04-03 15:12 585 查看

一、包装类型
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 deleteItems(Integer[] items_id) throws Exception {
//调用service来批量删除商品
return "success";

}


2、页面代码

<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>


*3、数组类型参数绑定总结

页面中定义的
name
属性值和
controller
中的参数名称相同。

三、
List
参数绑定

需求:

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

controller
方法定义:

(1)进入批量商品修改页面(页面样式参考商品列表实现)

(2)批量修改商品提交

List
接收页面提交的批量数据,通过包装
pojo
接收,在包装pojo中定义
list<pojo>
属性

1、
controller
中的方法


//批量修改商品页面,将商品信息查询出来,在页面中编辑商品信息
@RequestMapping("/editItemsQuery")
public ModelAndView editItemsQuery(ItemsQueryVo itemsQueryVo) throws Exception {
//调用service查找数据库,查询商品列表,这里先使用静态的数据模拟
List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);

ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute方法
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/editItemsQuery");
return modelAndView;
}
//批量修改商品提交
//通过ItemsQueryVo接收批量商品信息,将商品信息存储到itemsQueryVo中的itemsList中
@RequestMapping("/editItemsAllSubmit")
public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception {

return "success";
}


2、页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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">
<script type="text/javascript">
function editItemsAllSubmit() {
//提交form
document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsAllSubmit.action";
document.itemsForm.submit();
}
function queryItems() {
//提交form
document.itemsForm.action="${pageContext.request.contextPath }/items/queryItems.action";
document.itemsForm.submit();
}
</script>
<title>查询商品列表</title>
</head>
<body>
<form name="itemsForm" action="${pageContext.request.contextPath }/items/queryItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td> 商品的名称:<input name="itemsCustom.name" /> </td>
<td>
<input type="button" 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" varStatus="status">
<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>

<td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>


3、
List
类型绑定总结


不是直接在
controller
方法形参上定义
List
参数,而是在包装类中定义
List
属性,在
controller
方法中用包装类来接收,然后在页面中通过
name
属性值来调用。

4、部署调试截图



四、
Map
类型参数绑定

Map
类型参演书绑定方法和
List
类型绑定方法类似,也是在包装类中定义
Map
属性,在
controller
方法中用包装类来接收,然后在页面中通过
name
属性值来调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringMVC java