史上最简单的 Spring MVC 教程(八)

来源:互联网 发布:wsn网络分析器sniffer 编辑:程序博客网 时间:2024/06/05 23:47

目录(?)[+]

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>();    // 初始化 id    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() {        // 将 map 对象转换为 list 结合        return new ArrayList(map.values());    }    // 获得一个 Person 对象    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;    // 注入 service 层    @RequestMapping(value = "/person/all")    public String findAll(Map<String, Object> model) {     // 声明 model 用来传递数据        List<Person> personList = ps.findAll();        model.put("personList", personList);              // 通过这一步,JSP 页面就可以访问 personList        return "/person/jPersonList";                    // 跳转到 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);         // 将数据放到 response        return "/person/jPersonUpdate";    }    @RequestMapping("/person/updatePersonList")    public String updatePersonList(Person p) {        if (p.getId() == null) {            ps.insert(p);   // 调用 Service 层方法,插入数据        } else {            ps.update(p);   // 调用 Service 层方法,更新数据        }        return "redirect:/person/all.action";        // 转向人员列表 action    }    @RequestMapping("/person/deleteById")    public String deleteById(Integer id){        ps.deleteById(id);        return "redirect:/person/all.action";        // 转向人员列表 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;    // 注入 service 层    @RequestMapping(value = "/person/all")    public String findAll(Map<String, Object> model) {     // 声明 model 用来传递数据        List<Person> personList = ps.findAll();        model.put("personList", personList);              // 通过这一步,JSP 页面就可以访问 personList        return "/person/jPersonList";                    // 跳转到 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);         // 将数据放到 response        return "/person/jPersonUpdate";    }    @RequestMapping("/person/updatePersonList")    public String updatePersonList(Person p) {  // 更新人员信息        if (p.getId() == null) {            ps.insert(p);   // 调用 Service 层方法,插入数据        } else {            ps.update(p);   // 调用 Service 层方法,更新数据        }        return "redirect:/person/all.action";        // 转向人员列表 action    }    @RequestMapping("/person/deleteById")    public String deleteById(Integer id){  // 删除单条记录        ps.deleteById(id);        return "redirect:/person/all.action";        // 转向人员列表 action    }    @RequestMapping("/person/deleteMuch")    public String deleteMuch(@RequestParam("id") Integer[] ids){  // 批量删除记录        for (Integer delId : ids){            ps.deleteById(delId);        }        return "redirect:/person/all.action";        // 转向人员列表 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";        // 转向人员列表 action    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

第二步:修改 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>   <!-- 调用 JavaScript -->        </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 教程 <- 目录 —— ☆☆☆ ————

0 0