SpringMVC引數多種接收
阿新 • • 發佈:2018-12-17
SpringMVC引數接收
一.使用陣列接收資料
頁面:接收多選框的值
二.後臺
@RequestMapping("/saveUser.action")
public void saveUser1(User user,int[] enjoy){
System.out.println(user);
System.out.println(Arrays.toString(enjoy));
}
二.使用數集合LIST接收資料
注意:使用集合接收資料時 集合必須封裝在實體類物件中, 可做工具使用
1.封裝實體類
package com.igeek.ssm.util.pojo; import java.util.List; import com.igeek.ssm.pojo.Item; import com.igeek.ssm.pojo.User; /** * @author www.igeehome.com * * TODO * * 2018年10月24日下午7:42:35 */ public class QueryVo { private Item item; private Integer [] ids; private List<User> users; /** * @return the item */ public Item getItem() { return item; } /** * @param item the item to set */ public void setItem(Item item) { this.item = item; } /** * @return the ids */ public Integer[] getIds() { return ids; } /** * @param ids the ids to set */ public void setIds(Integer[] ids) { this.ids = ids; } /** * @return the users */ public List<User> getUsers() { return users; } /** * @param users the users to set */ public void setUsers(List<User> users) { this.users = users; } }
2.頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <form action="saveUser.action" method="post"> <hr/> <p>使用者名稱:<input type="text" name="users[0].username" /></p> <p>生日:<input type="date" name="users[0].birthday"/></p> <p>性別:<input type="radio" name="users[0].sex" value="1"/>男 <input type="radio" name="users[0].sex" value="2"/>女 </p> <p>地址:<input type="text" name="users[0].address"/></p> <hr/> <p>使用者名稱:<input type="text" name="users[1].username" /></p> <p>生日:<input type="date" name="users[1].birthday"/></p> <p>性別:<input type="radio" name="users[1].sex" value="1"/>男 <input type="radio" name="users[1].sex" value="2"/>女 </p> <p>地址:<input type="text" name="users[1].address"/></p> <hr/> <p>使用者名稱:<input type="text" name="users[2].username" /></p> <p>生日:<input type="date" name="users[2].birthday"/></p> <p>性別:<input type="radio" name="users[2].sex" value="1"/>男 <input type="radio" name="users[2].sex" value="2"/>女 </p> <p>地址:<input type="text" name="users[2].address"/></p> <p><input type="submit" value="提交"/></p> </form> </body> </html>
3.後臺controller
@RequestMapping("/queryItem")
public ModelAndView queryVo(QueryVo vo){
System.out.println("查詢引數name:"+vo.getItem().getName());
System.out.println("查詢引數id:"+vo.getItem().getId());
List<Item> itemList = itemService.queryByQueryVo(vo);
ModelAndView modelAndView = new ModelAndView("itemList");
modelAndView.addObject("itemList", itemList);
return modelAndView;
}
三.批量刪除資料,多選框,用陣列接收資料
1.頁面
通過js實現多選
將獲取的ids拼接到URL後面
<%@ 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 deleteSelected(){
var checkBoxs = document.getElementsByName("ids");
//定義一個url
var url = "deleteItems.action?x=1"
for(var i = 0;i<checkBoxs.length;i++){
//console.log(checkBoxs[i]+":"+checkBoxs[i].checked);
if(checkBoxs[i].type=='checkbox'){
if(checkBoxs[i].checked){
console.log(checkBoxs[i].value);
url += "&ids="+checkBoxs[i].value
}
}
}
console.log(url);
//通過js傳送刪除請求
location.href=url;
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryItem.action" method="post">
查詢條件:
<table width="100%" border=1>
<tr>
<td>商品id<input type="text" name="item.id"/> </td>
<td>商品名稱<input type="text" name="item.name"/> </td>
<td><input type="submit" value="查詢"/></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="${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}">修改</a></td>
</tr>
</c:forEach>
</table>
<input type = "button" onclick="deleteSelected()" value="刪除所選"/>
</form>
</body>
</html>
2.後臺
@RequestMapping("/deleteItems")
public void deleteItems(HttpServletResponse response,int [] ids,QueryVo vo) throws IOException{
System.out.println(Arrays.toString(ids));
System.out.println(Arrays.toString(vo.getIds()));
response.sendRedirect("itemList.action");
}
3.實體類
package com.igeek.ssm.util.pojo;
import java.util.List;
import com.igeek.ssm.pojo.Item;
import com.igeek.ssm.pojo.User;
/**
* @author www.igeehome.com
*
* TODO
*
* 2018年10月24日下午7:42:35
*/
public class QueryVo {
private Item item;
private Integer [] ids;
private List<User> users;
/**
* @return the item
*/
public Item getItem() {
return item;
}
/**
* @param item the item to set
*/
public void setItem(Item item) {
this.item = item;
}
/**
* @return the ids
*/
public Integer[] getIds() {
return ids;
}
/**
* @param ids the ids to set
*/
public void setIds(Integer[] ids) {
this.ids = ids;
}
/**
* @return the users
*/
public List<User> getUsers() {
return users;
}
/**
* @param users the users to set
*/
public void setUsers(List<User> users) {
this.users = users;
}
}
四. 接收引數為引用型別的資料(類中的屬性是類的引數)
1.頁面
<%@ 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>
<!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" -->
<!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> -->
<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><input type="text" name="createtime"
value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
</tr>
<%--
<tr>
<td>商品圖片</td>
<td>
<c:if test="${item.pic !=null}">
<img src="/pic/${item.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="pictureFile"/>
</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>
2.後臺控制器
@RequestMapping("/queryItem")
public ModelAndView queryVo(QueryVo vo){
System.out.println("查詢引數name:"+vo.getItem().getName());
System.out.println("查詢引數id:"+vo.getItem().getId());
List<Item> itemList = itemService.queryByQueryVo(vo);
ModelAndView modelAndView = new ModelAndView("itemList");
modelAndView.addObject("itemList", itemList);
return modelAndView;
}
3.實體類
package com.igeek.ssm.util.pojo;
import java.util.List;
import com.igeek.ssm.pojo.Item;
import com.igeek.ssm.pojo.User;
/**
* @author www.igeehome.com
*
* TODO
*
* 2018年10月24日下午7:42:35
*/
public class QueryVo {
private Item item;
private Integer [] ids;
private List<User> users;
/**
* @return the item
*/
public Item getItem() {
return item;
}
/**
* @param item the item to set
*/
public void setItem(Item item) {
this.item = item;
}
/**
* @return the ids
*/
public Integer[] getIds() {
return ids;
}
/**
* @param ids the ids to set
*/
public void setIds(Integer[] ids) {
this.ids = ids;
}
/**
* @return the users
*/
public List<User> getUsers() {
return users;
}
/**
* @param users the users to set
*/
public void setUsers(List<User> users) {
this.users = users;
}
}
五.引數預設defaultValue 與引數required=true 必須傳
@RequestMapping("/simpleparam.action")
public void reviceSimpleParam(int id,@RequestParam(value="username",defaultValue="igeek123",required=true) String name){
System.out.println("reviceSimpleParam:"+id+"-"+name);
}
六.獲取ServletContext
@RequestMapping("/getWebElement.action")
public void getWebElement(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException, ServletException{
String name = request.getParameter("name");
System.out.println("通過request獲取請求引數name:"+name);
//通過request轉發,
//request.getRequestDispatcher("123.jsp").forward(request, response);
//通過response重定向
//response.sendRedirect("123.jsp");
System.out.println(session.getId());
//ServletContext物件不能通過引數傳遞
//通過session獲取ServletContext物件
ServletContext application = session.getServletContext();
System.out.println(application);
}