1.javabean封裝的對象類型list
//批量修改items
@RequestMapping(value = "/item/updateItems")
public ModelAndView updateItems(WrapperItem wrapperItem) {
System.out.print("wrapperItem:" + wrapperItem.toString());
// Items item = itemService.selectByPrimaryKey(id);
ModelAndView modelAndView = new ModelAndView();
// 設(shè)置數(shù)據(jù)到模型中
modelAndView.addObject("username", "成功");
// 設(shè)置視圖jsp犁嗅,需要設(shè)置視圖的物理地址
modelAndView.setViewName("index1");
return modelAndView;
}
package com.company.combine.model;
import java.util.List;
/**
* Created by liuqun on 2017/8/15.
*/
public class WrapperItem {
private Items items;
private List<Items> itemsList;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public List<Items> getItemsList() {
return itemsList;
}
public void setItemsList(List<Items> itemsList) {
this.itemsList = itemsList;
}
@Override
public String toString() {
return "WrapperItem{" +
"items=" + items +
", itemsList=" + itemsList +
'}';
}
}
<%@ 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>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem" method="post">
查詢條件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查詢"/></td>
</tr>
</table>
</form>
商品列表:
<%--<form action="${pageContext.request.contextPath }/item/deleteItems" method="post">--%>
<form action="${pageContext.request.contextPath }/item/updateItems" method="post">
<table width="100%" border=1>
<tr>
<td><input type="checkbox" name="ids" value=""/></td>
<td>商品名稱</td>
<td>商品價格</td>
<td>生產(chǎn)日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="checkbox" name="ids" value="${item.id}"/></td>
<td> <input type="text" name="itemsList[${s.index}].name" value="${item.name}"/></td>
<td><input type="text" name="itemsList[${s.index}].price" value="${item.price}"/></td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td><input type="text" name="itemsList[${s.index}].detail" value="${item.detail}"/></td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="刪除"/>
<input type="submit" value="修改"/>
</form>
</body>
</html>
package com.company.combine.model;
import java.util.Date;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", pic='" + pic + '\'' +
", createtime=" + createtime +
", detail='" + detail + '\'' +
'}';
}
}
打印結(jié)果
wrapperItem:WrapperItem{items=null, itemsList=[Items{id=null, name='臺式機(jī)臺式111', price=3000.0, pic='null', createtime=null, detail='質(zhì)量很好'}, Items{id=null, name='筆記本111', price=6000.0, pic='null', createtime=null, detail='筆記本性能好犬金,質(zhì)量好R谡簟!;肼辍=葡唷茶袒!'}, Items{id=null, name='背包111', price=200.0, pic='null', createtime=null, detail='名牌背包溯壶,容量大質(zhì)量好A鸵佟F越肌!女蜈!'}]}
2.數(shù)組類型
//數(shù)組
@RequestMapping(value = "/item/deleteItems")
public ModelAndView deleteItems(Integer[] ids) {
System.out.print("id:" + Arrays.toString(ids));
// Items item = itemService.selectByPrimaryKey(id);
ModelAndView modelAndView = new ModelAndView();
// 設(shè)置數(shù)據(jù)到模型中
modelAndView.addObject("username", "成功");
// 設(shè)置視圖jsp持舆,需要設(shè)置視圖的物理地址
modelAndView.setViewName("index1");
return modelAndView;
}
<%@ 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>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem" method="post">
查詢條件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查詢"/></td>
</tr>
</table>
</form>
商品列表:
<form action="${pageContext.request.contextPath }/item/deleteItems" method="post">
<%--<form action="${pageContext.request.contextPath }/item/updateItems" method="post">--%>
<table width="100%" border=1>
<tr>
<td><input type="checkbox" name="ids" value=""/></td>
<td>商品名稱</td>
<td>商品價格</td>
<td>生產(chǎn)日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="checkbox" name="ids" value="${item.id}"/></td>
<td> <input type="text" name="name" value="${item.name}"/></td>
<td><input type="text" name="price" value="${item.price}"/></td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td><input type="text" name="detail" value="${item.detail}"/></td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="刪除"/>
<input type="submit" value="修改"/>
</form>
</body>
</html>
打印結(jié)果
id:[1, 2]
- 包裝類類型以及簡單類型
@RequestMapping(value = "/updateitem.action")
public ModelAndView updateItem(Integer id,String name,Float price,String pic,String detail,Items item, Model model) {
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("price:" + price);
System.out.println("pic:" + pic);
System.out.println("detail:" + detail);
System.out.println("items:" + item.toString());
System.out.println("model:" + model.toString());
if (item != null) {
item.setCreatetime(new Date());
}
int id1 = itemService.updateByPrimaryKey(item);
System.out.println("id:" + id1);
ModelAndView modelAndView = new ModelAndView();
// 設(shè)置數(shù)據(jù)到模型中
modelAndView.addObject("username", "修改成功");
// 設(shè)置視圖jsp,需要設(shè)置視圖的物理地址
modelAndView.setViewName("index1");
return modelAndView;
}
<%@ 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"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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" src="<%=basePath%>js/jquery.min.js"></script>
<script type="text/javascript">
<%--$(function(){--%>
<%--// alert(1);--%>
<%--var params = '{"id": 1,"name": "測試商品","price": 99.9,"detail": "測試商品描述","pic": "123456.jpg","more":{"one":"one"}}';--%>
<%--// $.post(url,params,function(data){--%>
<%--//回調(diào)--%>
<%--// },"json");//--%>
<%--$.ajax({--%>
<%--url : "${pageContext.request.contextPath }/json.action",--%>
<%--data : params,--%>
<%--contentType : "application/json;charset=UTF-8",//發(fā)送數(shù)據(jù)的格式--%>
<%--type : "post",--%>
<%--dataType : "json",//回調(diào)--%>
<%--success : function(data){--%>
<%--// alert(data.name);--%>
<%--},--%>
<%--});--%>
<%--});--%>
</script>
</head>
<body>
<!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" -->
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post" >
<%--<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post" enctype="multipart/form-data">--%>
<input type="hidden" name="id" value="${item.id }" /> 修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td><input type="text" name="name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品價格</td>
<td><input type="text" name="price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品日期</td>
<td><input type="text" name="createtime" value="${item.createtime }" /></td>
</tr>
<tr>
<td>商品圖片</td>
<td>
<c:if test="${item.pic != null}">
![](/pic/${item.pic})
<br/>
</c:if>
<input type="file" name="pic"/>
</td>
</tr>
<tr>
<td>商品簡介</td>
<td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
打印結(jié)果:
id:1
name:臺式機(jī)臺式1111111
price:3000.0112
pic:
detail:質(zhì)量很好1111
items:Items{id=1, name='臺式機(jī)臺式1111111', price=3000.0112, pic='', createtime=null, detail='質(zhì)量很好1111'}
model:{items=Items{id=1, name='臺式機(jī)臺式1111111', price=3000.0112, pic='', createtime=null, detail='質(zhì)量很好1111'}, org.springframework.validation.BindingResult.items=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
id:1