1 前言
在史上最简单的 Spring MVC 教程(七)中,咱们已经实现了“人员列表”的修改功能,那么接下来,在本篇博文中,咱们继续实现“人员列表”中人员信息的删除功能,包括删除单条记录和批量删除记录。
2 注解示例 - 删除
老规矩,首先给出项目结构图:
2.1 删除单条记录
第一步:在 Service 层(PersonService)中新建删除单体记录的方法
package spring.mvc.service;import org.springframework.stereotype.Service;import spring.mvc.domain.Person;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by 维C果糖 on 2017/1/26. */@Servicepublic class PersonService { private static Map<Integer, Person> map = new HashMap<Integer, Person>(); private static Integer id = 0; static { for (int i = 0; i < 10; i++) { Person p = new Person(); p.setId(id++); p.setName("Charie" + i); p.setAge(10 + i); map.put(i, p); } } public List<Person> findAll() { return new ArrayList(map.values()); } public Person get(Integer id) { return map.get(id); } public void insert(Person p) { id++; p.setId(id); map.put(id, p); } public void update(Person p) { map.put(p.getId(), p); } public void deleteById(Integer id) { map.remove(id); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
第二步:在控制器(PersonController)中添加新方法
package spring.mvc.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import spring.mvc.domain.Person;import spring.mvc.service.PersonService;import javax.annotation.Resource;import java.util.List;import java.util.Map;/** * Created by 维C果糖 on 2017/1/26. */@Controllerpublic class PersonController { @Resource PersonService ps; @RequestMapping(value = "/person/all") public String findAll(Map<String, Object> model) { List<Person> personList = ps.findAll(); model.put("personList", personList); return "/person/jPersonList"; } @RequestMapping("/person/toCreatePersonInfo") public String toCteatePersonInfo() { return "/person/jPersonCreate"; } @RequestMapping("/person/toUpdatePersonInfo") public String toUpdatePersonInfo(Integer id, Model model) { Person p = ps.get(id); model.addAttribute("p", p); return "/person/jPersonUpdate"; } @RequestMapping("/person/updatePersonList") public String updatePersonList(Person p) { if (p.getId() == null) { ps.insert(p); } else { ps.update(p); } return "redirect:/person/all.action"; } @RequestMapping("/person/deleteById") public String deleteById(Integer id){ ps.deleteById(id); return "redirect:/person/all.action"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
第三步:修改 jPersonList.jsp 页面,添加“删除”选项
<%-- Created by IntelliJ IDEA. User: 维C果糖 Date: 2017/1/27 Time: 00:00 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>PersonList</title></head><body> <form action="${pageContext.request.contextPath}/personform.action" method="post"> <div style="padding:20px;"> 人员列表 </div> <div style="padding-left:40px;"> <a href="/springmvc-annotation/person/toCreatePersonInfo.action">新增</a> </div> <table border="1"> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </tr> <c:forEach items="${personList}" var="p"> <tr> <td>${p.id}</td> <td>${p.name}</td> <td>${p.age}</td> <td> <a href=/springmvc-annotation/person/toUpdatePersonInfo.action?id=${p.id}>修改</a> <a href=/springmvc-annotation/person/deleteById.action?id=${p.id}>删除</a> </td> </tr> </c:forEach> </table> </form></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
在完成以上步骤后,启动 tomcat 服务器,然后访问 http://localhost:8080/springmvc-annotation/person/all.action,页面如下所示:
然后,选择想要删除的记录,在这里,咱们选择编号为10的记录进行测试,点击“删除”,页面将会重新跳转到 http://localhost:8080/springmvc-annotation/person/all.action,并显示删除记录后的人员列表,页面显示如下所示:
2.2 批量删除记录
第一步:在控制器(PersonController)中添加批量删除的方法
package spring.mvc.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import spring.mvc.domain.Person;import spring.mvc.service.PersonService;import javax.annotation.Resource;import java.util.List;import java.util.Map;/** * Created by 维C果糖 on 2017/1/26. */@Controllerpublic class PersonController { @Resource PersonService ps; @RequestMapping(value = "/person/all") public String findAll(Map<String, Object> model) { List<Person> personList = ps.findAll(); model.put("personList", personList); return "/person/jPersonList"; } @RequestMapping("/person/toCreatePersonInfo") public String toCteatePersonInfo() { return "/person/jPersonCreate"; } @RequestMapping("/person/toUpdatePersonInfo") public String toUpdatePersonInfo(Integer id, Model model) { Person p = ps.get(id); model.addAttribute("p", p); return "/person/jPersonUpdate"; } @RequestMapping("/person/updatePersonList") public String updatePersonList(Person p) { if (p.getId() == null) { ps.insert(p); } else { ps.update(p); } return "redirect:/person/all.action"; } @RequestMapping("/person/deleteById") public String deleteById(Integer id){ ps.deleteById(id); return "redirect:/person/all.action"; } @RequestMapping("/person/deleteMuch") public String deleteMuch(@RequestParam("id") Integer[] ids){ for (Integer delId : ids){ ps.deleteById(delId); } return "redirect:/person/all.action"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
如上所示,在函数 deleteMuch 中,为了保证页面传递过来的参数与定义在控制器中的方法的参数名称的一致性,咱们用注解 @RequestParam(“id”) 将参数 ids 内容封装到 id 中。在这里,咱们还有另外一种参数的传递方式,即直接将 deleteMuch 方法中的参数类型定义为 String 类型,这样当spring MVC 框架在接收到页面传递过来的多个同名参数值后,其会用逗号将参数值依次拼接成为一个字符串。
特别注意:当封装多个同名参数值时,如果方法中的参数类型为 Integer 、Double 或者 Date 时,Spring MVC 框架为了容错,只会保留第一个参数值,而不会抛出错误。
在此,作者给出第二种传参方法以供参考:
@RequestMapping("/person/deleteMuch") public String deleteMuch(String id){ for (String delId : id.split(",")){ ps.deleteById(Integer.parseInt(delId)); } return "redirect:/person/all.action"; }
第二步:修改 jPersonList.jsp 页面,添加“批量删除”选项、复选框及 JavaScript 函数
<%-- Created by IntelliJ IDEA. User: 维C果糖 Date: 2017/1/29 Time: 15:30 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>PersonList</title> <script language="JavaScript"> function deleteMuch() { document.forms[0].action = "${pageContext.request.contextPath}/person/deleteMuch.action"; document.forms[0].submit(); } </script></head><body> <form method="post"> <div style="padding:20px;"> 人员列表 </div> <div style="padding-left:40px;padding-bottom: 10px;"> <a href="/springmvc-annotation/person/toCreatePersonInfo.action">新增</a> <a href="#" onclick="deleteMuch()">批量删除</a> </div> <table border="1"> <tr> <td>选择</td> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </tr> <c:forEach items="${personList}" var="p"> <tr> <td> <input type="checkbox" name="id" value="${p.id}"/> </td> <td>${p.id}</td> <td>${p.name}</td> <td>${p.age}</td> <td> <a href=/springmvc-annotation/person/toUpdatePersonInfo.action?id=${p.id}>修改</a> <a href=/springmvc-annotation/person/deleteById.action?id=${p.id}>删除</a> </td> </tr> </c:forEach> </table> </form></body></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
在完成以上步骤后,启动 tomcat 服务器,然后访问 http://localhost:8080/springmvc-annotation/person/all.action,页面如下所示:
然后,选择想要删除的记录,在这里,咱们选择编号为 0 和 1 的记录进行测试,点击“批量删除”,页面将会重新跳转到http://localhost:8080/springmvc-annotation/person/all.action,并显示批量删除记录后的人员列表,页面显示如下所示:
———— ☆☆☆ —— 返回 -> 史上最简单的 Spring MVC 教程 <- 目录 —— ☆☆☆ ————