springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定
一、包装类型pojo参数绑定:
需求:商品查询controller方法中实现商品查询条件传入。
实现方法:
1)在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数。
2)在形参中让包装类型的pojo接收查询条件参数。
做法:参数名和包装pojo中的属性一致即可;
(本例中:<input name="itemsCustom.name" />传递参数 和 ItemsQueryVo属性名itemsCustom一致);
二、数组绑定:
需求:商品批量删除,用户在页面选择多个商品,批量删除。
做法:将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id。
(本例中deleteItems(Integer[] item_id) item_id用来接收checkbox的name为item_id数组)
一、二实现如下:
ItemsController:
// 商品查询
@RequestMapping("/findItems")
public ModelAndView findItems(ItemsQueryVo itemsQueryVo) throws Exception { List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo); ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("items/itemsList");
return modelAndView;
} // 批量删除 商品信息
@RequestMapping("/deleteItems")
public String deleteItems(Integer[] item_id) throws Exception{
// 调用service批量删除商品
// ... for(int id : item_id){
System.out.println("待删除的商品id:---------------->>" + id);
} return "success";
}
ItemsQueryVo:
/**
* 商品包装对象
* @author chengyu
*
*/
public class ItemsQueryVo {
//商品信息
private Items items; //为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom; //批量商品信息
private List<ItemsCustom> itemsList; ...
}
查询和批量删除itemsList.jsp:
<%@ 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">
<title>查询商品列表</title>
<script>
function deleteItems(){
document.itemsForm.action = "${pageContext.request.contextPath }/items/deleteItems.action";
document.itemsForm.submit();
} function queryItems(){
document.itemsForm.action = "${pageContext.request.contextPath }/items/findItems.action";
document.itemsForm.submit();
}
</script>
</head>
<body>
<form name="itemsForm" action="" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>
商品名称:<input name="itemsCustom.name" />
<input type="button" 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="item_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>

三、List绑定:
需求:通常在需要批量提交数据时,将提交的数据绑定到list<pojo>中,本例子中:批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。
做法:使用List接收页面提交的批量数据,通过Controller形参中包装pojo接收,在包装pojo中定义List<pojo>属性;
(本例中在ItemsQueryVo中定义itemsList属性接收页面提交的批量商品;status.index定义了下标从0开始;.name/.price..对应了List<ItemsCustom>中ItemsCustom的属性名)
ItemsController:
// 批量修改商品页面,将商品信息查询出来,在页面中可以编辑商品信息
@RequestMapping("/editItemsQuery")
public ModelAndView editItemsQuery(ItemsQueryVo itemsQueryVo) throws Exception{
List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("items/editItemsQuery");
return modelAndView;
} // 批量修改商品提交
// 通过ItemsQueryVo接收批量提交的商品信息,将商品信息存储到itemsQueryVo中itemsList属性中。
@RequestMapping("/editItemsAllSubmit")
public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception { return "success";
}
ItemsQueryVo.java:
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
//批量商品信息
private List<ItemsCustom> itemsList;
}
editItemsQuery.jsp:
<%@ 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">
<title>查询商品列表-可批量编辑</title>
<script type="text/javascript">
function editItemsAllSubmit(){
//提交form
document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsAllSubmit.action";
document.itemsForm.submit();
}
function editItemsQuery(){
//提交form
document.itemsForm.action="${pageContext.request.contextPath }/items/editItemsQuery.action";
document.itemsForm.submit();
}
</script>
</head>
<body>
<form name="itemsForm" action="" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>
商品名称:<input name="itemsCustom.name" />
</td>
<td>
<input type="button" value="查询" onclick="editItemsQuery()"/>
<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>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

四、Map绑定:
在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。
包装类中定义Map对象如下:
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>
Controller方法定义如下:
public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getIteminfo());
}
springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定的更多相关文章
- Go语言学习笔记(三)数组 & 切片 & map
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 数组 Arrays 数组是同一种数据类型的固定长度的序列. 数组是值类型,因此改变副本的值,不会改变本身的值: 当 ...
- Hibernate学习---第六节:数组&list&map&set的映射配置
1.实体类,代码如下: package learn.hibernate.bean; import java.util.Date; import java.util.HashMap; import ja ...
- Go语言学习之4 递归&闭包&数组切片&map&锁
主要内容: 1. 内置函数.递归函数.闭包2. 数组与切片3. map数据结构4. package介绍 5. 排序相关 1. 内置函数.递归函数.闭包 1)内置函数 (1). close:主要用来关闭 ...
- springmvc学习第二天
一.pojo Spring mvc 会按请求参数名和pojo属性名进行自动匹配,自动为该对象填充属性值,并且支持级联属性 表单: <form action="springmvc/tes ...
- SpringMVC学习(四)———— 数据回显与自定义异常处理器
一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什么是数据回显? 在信息校验时,如果发生校验错误,那么把校验的数据信息,依然停留在当前页面, ...
- 学习Golang语言(6):类型--切片
学习Golang语言(1): Hello World 学习Golang语言(2): 变量 学习Golang语言(3):类型--布尔型和数值类型 学习Golang语言(4):类型--字符串 学习Gola ...
- springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定
springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定 标签: springmvc springmvc学习笔记12-springmvc注解开发之包装类型參数绑定 需求 实现方 ...
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- SpringMVC由浅入深day01_12.4 pojo绑定_12.5自定义参数绑定实现日期类型绑定_12.6集合类
12.4 pojo绑定 页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo. 页面定义: controller的pojo形参的定义: 打断点测试 ...
随机推荐
- Superset 制作图表
参考资料: http://lxw1234.com/archives/2018/03/904.htm https://wenku.baidu.com/view/49ffdf8b77eeaeaad1f34 ...
- 基于GUI的简单聊天室01
运用了Socket编程,gui,流的读入和写出,线程控制等 思路: 1.首先是在客户端中先建立好聊天的GUI 2.建立服务器端,设置好端口号(用SocketServer),其中需要两个boolean变 ...
- DevExpress v18.1新版亮点——WPF篇(一)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v18.1 的新功能,快来下载试用新版本!点击下载& ...
- spring集成redis——主从配置以及哨兵监控
Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...
- jQuery的noConflict以及插件扩展
一.noConflict函数 JavaScript有很多插件,如果jQuery对象的$与其他插件冲突,我们可以使用noConflict()方法去掉$或者使用其他的符号代替 注:noConflict() ...
- 把腾讯云的ubuntu16.04升级到18.04
腾讯云买的服务器也没怎么弄,正好重装一下玩乐了. 1. 重装系统,在腾讯云里先停机,然后重装系统,目前最高是ubuntu16.04.为什么选择Ubuntu?因为,因为习惯吧,之前学习laravel就是 ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- ios之开发屏幕适配和系统版本适配
ios软件开发过程中很重要的一点是对系统和屏幕进行适配对系统的适配主要是IOS7以后和之前以及IOS8新增特性,屏幕适配主要是对不同设备采用不同的布局以最佳展示效果展现给用户. 针对系统的适配: IO ...
- spring读取propertyes 新方法
<context:property-placeholder location="classpath:mysql.properties"/> <util:prope ...
- 【论文解读】行人检测:What Can Help Pedestrian Detection?(CVPR'17)
前言 本篇文章出自CVPR2017,四名作者为Tsinghua University,Peking University, 外加两名来自Megvii(旷视科技)的大佬. 文章中对能够帮助行人检测的ex ...