1 傳遞基本資料類型
A 頁面請求位址【帶參傳遞】
<a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >修改</a>
B controller層接收參數
package com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.Items;
import com.service.ItemService;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
// @RequestMapping:裡面放的是請求的url,和使用者請求的url進行比對
@RequestMapping("/itemEdit.action")
//頁面傳遞的參數id 直接該方法的參數 【請求頁面和該方法的名稱要一緻】
// Integer id <<========<< itemEdit.action?id=${item.id}
public ModelAndView queryById(Integer id) {
Items items = itemService.queryById(id);
// 建立ModelAndView,用來存放資料和視圖
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("item", items);
//在視圖解析器【springmvc.xml】裡面會進行拼接【傳回的jsp頁面】
modelAndView.setViewName("editItem");
return modelAndView;
}
}
2 傳遞POJO類【Items】
請求頁面【input的name屬性必須與POJO屬性名相同】
POJO類
package com.pojo;
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;
....get/set.....
}
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<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><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>
controller接收
//更新操作【如果隻需要傳回jsp頁面不傳遞資料,那麼傳回值類型可以為String 會自動拼接到jsp頁面】
@RequestMapping("/updateitem.action")
//參數名 要與 請求頁面的屬性的POJO類名稱相同
public String queryById(Items items) {
itemService.updateById(items);
//傳回updateSuccess.jsp頁面
return "updateSuccess";
}
3 傳遞包裝類POJO
包裝類POJO【包裝類裡面有一個POJO對象】
package com.pojo;
import java.util.Date;
public class QueryVo{
private Items items;
....get/set.....
}
請求頁面【input框的name屬性修改成了item.*】
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<input type="hidden" name="items.id" value="${item.id }" /> 修改商品資訊:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td><input type="text" name="items.name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品價格</td>
<td><input type="text" name="items.price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品簡介</td>
<td><textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="送出" />
</td>
</tr>
</table>
</form>
controller接收
// 綁定包裝資料類型
@RequestMapping("/queryItem")
public String queryItem(QueryVo queryVo) {
System.out.println(queryVo.getItem().getId());
System.out.println(queryVo.getItem().getName());
return "success";
}
4 傳遞數組【頁面選中多個checkbox向controller方法傳遞】
請求頁面:
<%@ 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 }/deleteItems.action" method="post">
商品清單:
<table width="100%" border=1>
<tr>
<td></td>
<td>商品名稱</td>
<td>商品價格</td>
<td>生産日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td><input type="checkbox" name="ids" 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 }/itemEdit.action?id=${item.id}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >修改</a></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="删除選中">
</form>
</body>
</html>
controller接收
package com.controller;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.Items;
import com.service.ItemService;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
//批量删除數組
@RequestMapping("/deleteItems.action")
public String deletes(Integer[] ids) {
for (Integer integer : ids) {
//System.out.println(integer);
//删除操作.......
}
return "success";
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL3AjNzEjNwQTMxMzNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
5 傳遞List集合【實作商品資料的批量修改。】 需要使用包裝類
包裝類:
package com.pojo;
import java.util.List;
public class QueryVo {
//Items是一個POJO類
private List<Items> ItemLists;
//.....get/set.......
}
請求頁面:【隻對name和price進行測試】
<%@ 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 }/updateItems.action" method="post">
商品清單:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td>商品價格</td>
<td>生産日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="text" name="ItemLists[${s.index }].name" value="${item.name }"></td>
<td><input type="text" name="ItemLists[${s.index }].price" value="${item.price }"></td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
</tr>
</c:forEach>
</table>
<input type="submit" value="批量更新">
</form>
</body>
</html>
controller接收
package com.controller;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.Items;
import com.pojo.QueryVo;
import com.service.ItemService;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
//批量更新List
@RequestMapping("/updateItems.action")
public String updates(QueryVo vo) {
List<Items> itemLists = vo.getItemLists();
for (Items items : itemLists) {
System.out.println(items.toString());
}
return "success";
}
}