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形参的定义: 打断点测试 ...
随机推荐
- python随机数,随机选择……random
import random from random import random, uniform, randint, randrange, choice, sample, shuffle list = ...
- Lightbox JS v2.0图片切换效果
代码下载
- JavaScript事件漫谈
内容概要: event对象,事件在多个浏览器中的兼容,事件的传播机制,JS自定义事件,jQuery的自定义事件的绑定与触发 Event对象 Event对象属于HTML DOM对象.Event 对象代表 ...
- Android RIL结构分析与移植
介绍 本文档对Android RIL部分的内容进行了介绍,其重点放在了Android RIL的原生代码部分. 包括四个主题: 1.Android RIL框架介绍 2.Android RIL与 Wind ...
- .Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”
一. 问题说明 最近.Net EF core 程序部署到服务器,服务器数据库安装的是SQL server 2008 R2,我本地用的的是SQL server 2014,在用到分页查询时报错如下: H ...
- 基础练习 Sine之舞
基础练习 Sine之舞 时间限制:1.0s 内存限制:512.0MB 问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他 ...
- ZooKeeper 集群环境搭建 (本机3个节点)
--------------------------------------------------------1.建立目录server1server1/dataDirserver1/dataLogD ...
- XDocument 使用
摘要: 正文: 1.引入XDocument的命名空间 using System.Xml.Linq; 2. List<CourseItem> to XML doc //List<Cou ...
- ListBox item Sort
将Rss内容读取到Listbox control中, 然后实现按照标题或发布日期进行排序. private void ListItemSort(string type) { if (type == & ...
- IDEA错误:Cannot start compilation: the output path is not specified for module "XXX".
错误是发生在从github上checkout自己的项目时.因为没有将配置文件一起上传,所以在运行java程序时有了这个报错: Cannot start compilation: the output ...