SpringMVC學習筆記(十)——包裝型別pojo、陣列、集合的引數繫結
阿新 • • 發佈:2019-01-31
部落格原始碼下載:戳我一下
一、包裝型別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
屬性值來呼叫。