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学习(6)-包装pojo类型、数组、list、Map类型参数绑定
- 【SpringMVC注解开发】案例驱动-包装类型pojo参数绑定 SpringMVC学习笔记(五)
- 【Mybatis升级版-04】包装类型pojo参数绑定
- SpringMVC之(三)包装类型pojo参数绑定
- springmvc中的对象、数组、集合类型的参数绑定
- 【SpringMVC整合MyBatis】案例驱动-包装类型pojo参数绑定
- springmvc(十三)springmvc注解开发-springmvc参数绑定-包装类型pojo参数绑定
- SpringMVC数组、集合类型参数绑定与异常处理器
- springmvc注解开发-springmvc参数绑定-包装类型pojo参数绑定
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- spring mvc表单中集合类型的参数绑定,含集合下标动态改变
- 【SpringMVC整合MyBatis】案例驱动-集合类型参数绑定
- ASP.NET MVC3如何用JQuery传递数组(集合)类型参数给JsonResult Action
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- ASP.NET MVC3如何用JQuery传递数组(集合)类型参数给JsonResult Action
- SpringMVC集合类型参数绑定
- Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)
- springmvc 接受数组,集合(list,set)类型的参数
- springmvc学习笔记(12)-springmvc注解开发之包装类型参数绑定
- springmvc学习笔记(12)-springmvc注解开发之包装类型参数绑定