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

来源:互联网 发布:汕头淘宝运营培训班 编辑:程序博客网 时间:2024/05/20 13:09

目录(?)[+]

1 前言

在史上最简单的 Spring MVC 教程(五)中,咱们已经实现了在spring MVC框架下运用注解的方式显示“人员列表”信息的功能。那么,在本篇博文中,咱们更进一步,继续实现“人员列表”的添加功能。

2 注解示例 - 添加

首先,给出项目结构图:

项目结构图

第一步:在 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 = 1;    // 利用静态块初始化数据    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());    }    // 新增人员信息    public void insert(Person p) {        id++;        p.setId(id);        map.put(id, p);    }}
  • 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
  • 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

第二步:在控制器(PersonController)中添加新方法

package spring.mvc.controller;import org.springframework.stereotype.Controller;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/updatePersonList")    public String updatePersonList(Person p){        ps.insert(p);                                // 调用 Service 层方法,插入数据        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
  • 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

由上面的代码可知,我们在 PersonController 中新建了两个方法,分别为:toCteatePersonInfo 和 updatePersonList,其中 toCteatePersonInfo 用于跳转到新增页面,updatePersonList 则用于调用 Service 层中的 insert 方法以及跳转到显示人员列表的页面。特别是在方法 updatePersonList 中,我们采用的是直接传递实体对象作为参数,在此,作者给出第二种传参方法以供参考:

 @RequestMapping("/person/updatePersonList")    public String updatePersonList(String name,Integer age){        Person p = new Person();        p.setName(name);        p.setAge(age);        ps.insert(p);                                // 调用 Service 层方法,插入数据        return "redirect:/person/all.action";        // 转向人员列表 action    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第三步:修改 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>            </tr>            <c:forEach items="${personList}" var="p">                <tr>                    <td>${p.id}</td>                    <td>${p.name}</td>                    <td>${p.age}</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
  • 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

第四步:新建 jPersonCreate.jsp 页面,用于输入数据

<%--  Created by IntelliJ IDEA.  User: 维C果糖  Date: 2017/1/28  Time: 21:12  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}/person/updatePersonList.action" method="post">    <div style="padding:20px;">        新增人员列表    </div>    <table>        <tr>            <td>姓名:</td>            <td><input type="text" name="name" value=""/></td>        </tr>        <tr>            <td>年龄:</td>            <td><input type="text" name="age" value=""/></td>        </tr>        <tr>            <td colspan="2"><input type="submit" name="btnOK" value="保存"/></td>        </tr>    </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
  • 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

在完成以上步骤后,启动 tomcat 服务器,然后访问 http://localhost:8080/springmvc-annotation/person/all.action,页面如下所示:

显示人员列表

点击“新增”,跳转页面后输入需要添加的人员信息,页面如下所示:

新增人员列表

最后,点击“保存”,页面将会重新跳转到 http://localhost:8080/springmvc-annotation/person/all.action,并显示新增人员信息后的人员列表,页面显示如下所示:

页面回显


———— ☆☆☆ —— 返回 -> 史上最简单的 Spring MVC 教程 <- 目录 —— ☆☆☆ ————

0 0