springmvc如何实现批量增加和删除呢?
首先看需求,一个jsp页面,可以选择多个学生进行删除:
<%@ 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>
<%
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
pageContext.setAttribute("basePath", basePath);
%>
<body>
<form action="${basePath }emp/deteteAll.action" method="post">
<table border="1" align="center">
<tr>
<th>编号</th>
<th>姓名</th>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="1"></td>
<td>张三1</td>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="2"></td>
<td>张三2</td>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="3"></td>
<td>张三3</td>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="4"></td>
<td>张三4</td>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="5"></td>
<td>张三5</td>
</tr>
<tr>
<td><input type="checkbox" name = "id" value="6"></td>
<td>张三6</td>
</tr>
<tr>
<td><input type="submit" value="删除"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
当我们提交后,springmvc如何获取所有选中的元素?
看一下我们的Action:
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
@RequestMapping(value = "/deteteAll",method=RequestMethod.POST)
public String deteteAll(Model model,int[] id) {
model.addAttribute("message", "批量删除员工成功");
System.out.println("需要删除的员工元素");
for (int i : id) {
System.out.print(i+" , ");
}
return "/jsp/ok.jsp";
}
}
我们只需要在业务方法中,添加数组属性,但是该数组的名字和jsp页面中的name属性的值要一致,这样才会获取选中的checkbox的value值。
这里说的是数组,那如果是一个List集合呢?
创建一个JaveBean,,:
/**
* 员工
* @author user
*
*/
public class Emp {
private String username;
private Double salary;
public Emp() {
// TODO Auto-generated constructor stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
在创建一个bean用来封装多个员工:
/**
* 封装多个员工
* @author user
*/
import java.util.ArrayList;
import java.util.List;
public class Bean {
private List<Emp> empList = new ArrayList<Emp>();
public Bean() {
// TODO Auto-generated constructor stub
}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
}
在我们的action的业务方法中传入封装了多个Emp的Bean参数。
/**
* 员工Actio批量增加
* 单例
* @author user
*
*/
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
/*
* 批量增加
*/
@RequestMapping(value = "/addAll",method=RequestMethod.POST)
public String deteteAll(Model model,Bean bean) {
model.addAttribute("message", "批量增加员工成功");
System.out.println(bean.getEmpList().size());
for (Emp emp : bean.getEmpList()) {
System.out.println(emp.getUsername()+" "+emp.getSalary());
}
return "/jsp/ok.jsp";
}
}
然后看一下我们的jsp页面如何写:
<%@ 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>
<%
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
pageContext.setAttribute("basePath", basePath);
%>
<body>
<form action="${basePath}emp/addAll.action" method="POST">
<table border="2" align="center">
<caption><h2>批量注册员工</h2></caption>
<tr>
<td><input type="text" name="empList[0].username" value="哈哈"/></td>
<td><input type="text" name="empList[0].salary" value="7000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[1].username" value="呵呵"/></td>
<td><input type="text" name="empList[1].salary" value="7500"/></td>
</tr>
<tr>
<td><input type="text" name="empList[2].username" value="班长"/></td>
<td><input type="text" name="empList[2].salary" value="8000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[3].username" value="键状哥"/></td>
<td><input type="text" name="empList[3].salary" value="8000"/></td>
</tr>
<tr>
<td><input type="text" name="empList[4].username" value="绿同学"/></td>
<td><input type="text" name="empList[4].salary" value="9000"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="批量注册"/>
</td>
</tr>
</table>
</form>
</body>
</html>
这里要注意他的name属性值,empList和我们的Bean中的list属性一样
.username和.salary和Emp中的属性一样,这样springmvc才会自动为list集合赋值。