SpringMVC多种参数接收

                                                 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);
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_无往而不胜_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值