1 需求分析(需求分析师)

功能分析:

1)添加联系人

2)修改联系人

3)删除联系人

4)查询所有联系人

2 需求设计(系统分析师/架构师/资深开发人员)

2.1设计实体(抽象实体)

联系人实体:

class Contact{
private String id;
private String name;
private String gender;
private int age;
private String phone;
private String email;
private String qq;
  public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
@Override
public String toString() {
return "Contact [age=" + age + ", email=" + email + ", gender="
+ gender + ", id=" + id + ", name=" + name + ", phone=" + phone
+ ", qq=" + qq + "]";
}
}

2.2设计“数据库”,(xml代替"数据库")

XML格式

contact.xml
<contactList>
<contact id="1">
<name>张三</name>
<gender>男</gender>
<age>20</age>
<phone>13433334444</phone>
<email>zs@qq.com</email>
<qq>43222222<qq>
</contact>
</contactList>

XML工具类

 public class XMLUtil {

     /**
* 读取xml文档方法
* @return
*/
public static Document getDocument(){
try {
Document doc = new SAXReader().read(new File("e:/contact.xml"));
return doc;
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* 写出到xml文档中
*/
public static void write2xml(Document doc){
try {
FileOutputStream out = new FileOutputStream("e:/contact.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

2.3设计涉及的接口

DAO接口(数据访问对象):实体对象的CRUD方法。

项目原则: 通常一个实体对象就会对应一个DAO接口和一个DAO实现类

DAO接口

interface ContactDao{
public void addContact(Contact contact);//添加联系人
public void updateContact(Contact contact);//修改联系人
public void deleteContact(String id);//删除联系人
public List<Contact> findAll(); //查询所有联系人
public Contact findById(String id);//根据编号查询联系人
}

DAO实现类

 public class ContactDaoImpl implements ContactDao {

     /**
* 添加联系人
*/
public void addContact(Contact contact) {
try {
File file = new File("e:/contact.xml");
Document doc = null;
Element rootElem = null;
if(!file.exists()){
/**
* 需求: 把contact对象保存到xml文件中
*/
//如果没有xml文件,则创建xml文件
doc = DocumentHelper.createDocument();
//创建根标签
rootElem = doc.addElement("contactList");
}else{
//如果有xml文件,则读取xml文件
doc = XMLUtil.getDocument();
//如果有xml文件,读取根标签
rootElem = doc.getRootElement();
} //添加contact标签
/**
* <contact id="1">
<name>eric</name>
<gender>男</gender>
<age>20</age>
<phone>1343333</phone>
<email>eric@qq.com</email>
<qq>554444</qq>
</contact>
*/
Element contactElem = rootElem.addElement("contact"); /**
* 由系统自动生成随机且唯一的ID值,赋值给联系人
*/
String uuid = UUID.randomUUID().toString().replace("-",""); contactElem.addAttribute("id", uuid);
contactElem.addElement("name").setText(contact.getName());
contactElem.addElement("gender").setText(contact.getGender());
contactElem.addElement("age").setText(contact.getAge()+"");
contactElem.addElement("phone").setText(contact.getPhone());
contactElem.addElement("email").setText(contact.getEmail());
contactElem.addElement("qq").setText(contact.getQq());
//把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* 删除联系人
*/
public void deleteContact(String id) {
try {
//1.读取xml文件
Document doc = XMLUtil.getDocument();
//2.查询需要删除id的contact
Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+id+"']");
//删除标签
if(contactElem!=null){
contactElem.detach();
} //3.把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} /**
* 查询所有联系人
*/
public List<Contact> findAll() {
//1.读取xml文件
Document doc = XMLUtil.getDocument(); //2.创建List对象
List<Contact> list = new ArrayList<Contact>();
//3.读取contact标签
List<Element> conList = (List<Element>)doc.selectNodes("//contact");
for(Element e:conList){
//创建COntact对象
Contact c = new Contact();
c.setId(e.attributeValue("id"));
c.setName(e.elementText("name"));
c.setGender(e.elementText("gender"));
c.setAge(Integer.parseInt(e.elementText("age")));
c.setPhone(e.elementText("phone"));
c.setEmail(e.elementText("email"));
c.setQq(e.elementText("qq"));
//把Contact放入list中
list.add(c);
}
return list;
} /**
* 根据编号查询联系人
*/
public Contact findById(String id) {
Document doc = XMLUtil.getDocument();
Element e = (Element)doc.selectSingleNode("//contact[@id='"+id+"']"); Contact c = null;
if(e!=null){
//创建COntact对象
c = new Contact();
c.setId(e.attributeValue("id"));
c.setName(e.elementText("name"));
c.setGender(e.elementText("gender"));
c.setAge(Integer.parseInt(e.elementText("age")));
c.setPhone(e.elementText("phone"));
c.setEmail(e.elementText("email"));
c.setQq(e.elementText("qq"));
}
return c;
} /**
* 修改联系人
*/
public void updateContact(Contact contact) {
/**
* 需求: 修改id值为2的联系人
* 1)查询id值为2的contact标签
* 2)修改contact标签的内容
*/
try {
//1.读取xml文件
Document doc = XMLUtil.getDocument(); Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+contact.getId()+"']"); //2.修改contact标签内容 contactElem.element("name").setText(contact.getName());
contactElem.element("gender").setText(contact.getGender());
contactElem.element("age").setText(contact.getAge()+"");
contactElem.element("phone").setText(contact.getPhone());
contactElem.element("email").setText(contact.getEmail());
contactElem.element("qq").setText(contact.getQq());
//3.把Document写出到xml文件
XMLUtil.write2xml(doc);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
//测试UUID
String uuid = UUID.randomUUID().toString().replace("-","");
System.out.println(uuid);
} }

2.4设计项目的目录结构

项目名称: contactSys_web

目录结构:

|- contactSys_web

  |-src

    |-gz.itcast.contactSys_web.entity : 存放实体对象

    |-gz.itcast.contactSys_web.dao : 存放dao的接口

    |-gz.itcast.contactSys_web.dao.impl: 存放dao的实现类

    |-gz.itcast.contactSys_web.servlet: 存放servlet的类

    |-gz.itcast.contactSys_web.test: 存放单元测试类

    |-gz.itcast.contactSys_web.util: 存放工具类

    |-gz.itcast.contactSys_web.exception: 存放自定义异常类

  |-WebRoot

    |-html文件

    |-images:目录。存放图片资源

    |-css:目录。存放css资源

    |-js:目录。存放js资源

3 编码实现(软件开发工程师/攻城狮)

开发顺序:

设计数据库-> 实体 -> DAO接口,DAO实现-> Servlet+html页面

3.1显示所有人的逻辑

public class ListContactServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.从xml中读取出联系人数据
ContactDao dao = new ContactDaoImpl();
List<Contact> list = dao.findAll(); //2.显示到浏览器
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); String html = ""; //shift+alt+A ^(.*)$ \1";
html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
html += "<html xmlns='http://www.w3.org/1999/xhtml'>";
html += "<head>";
html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
html += "<title>查询所有联系人</title>";
html += "<style type='text/css'>";
html += " table td{";
html += " /*文字居中*/";
html += " text-align:center;";
html += " }";
html += " ";
html += " /*合并表格的边框*/";
html += " table{";
html += " border-collapse:collapse;";
html += " }";
html += "</style>";
html += "</head>";
html += "";
html += "<body>";
html += "<center><h3>查询所有联系人</h3></center>";
html += "<table align='center' border='1' width='800px'>";
html += " <tr>";
html += " <th>编号</th>";
html += " <th>姓名</th>";
html += " <th>性别</th>";
html += " <th>年龄</th>";
html += " <th>电话</th>";
html += " <th>邮箱</th>";
html += " <th>QQ</th>";
html += " <th>操作</th>";
html += " </tr>";
if(list!=null){
for (Contact contact : list) {
html += " <tr>";
html += " <td>"+contact.getId()+"</td>";
html += " <td>"+contact.getName()+"</td>";
html += " <td>"+contact.getGender()+"</td>";
html += " <td>"+contact.getAge()+"</td>";
html += " <td>"+contact.getPhone()+"</td>";
html += " <td>"+contact.getEmail()+"</td>";
html += " <td>"+contact.getQq()+"</td>";
html += " <td><a href='"+request.getContextPath()+"/QueryContactServlet?id="+contact.getId()+"'>修改</a>&nbsp;<a href='"+request.getContextPath()+"/DeleteContactServlet?id="+contact.getId()+"'>删除</a></td>";
html += " </tr>";
}
}
html += " <tr>";
html += " <td colspan='8' align='center'><a href='"+request.getContextPath()+"/addContact.html'>[添加联系人]</a></td>";
html += " </tr>";
html += "</table>";
html += "</body>";
html += "</html>"; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.2添加联系人的逻辑

 public class AddContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.接收参数
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String qq = request.getParameter("qq"); //封装成Contact对象
Contact contact = new Contact();
contact.setName(name);
contact.setGender(gender);
contact.setAge(Integer.parseInt(age));
contact.setPhone(phone);
contact.setEmail(email);
contact.setQq(qq); //2.调用dao类的添加联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.addContact(contact); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.3修改前查询联系人的逻辑

 public class QueryContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.接收id
String id = request.getParameter("id"); //2.调用dao根据id查询联系人的方法
ContactDao dao = new ContactDaoImpl();
Contact contact = dao.findById(id); //3.把联系人显示到浏览器中
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter(); String html = ""; html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
html += "<html xmlns='http://www.w3.org/1999/xhtml'>";
html += "<head>";
html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
html += "<title>修改联系人</title>";
html += "</head>";
html += "";
html += "<body>";
html += "<center><h3>修改联系人</h3></center>";
html += "<form action='"+request.getContextPath()+"/UpdateContactServlet' method='post'>";
//注意:添加id的隐藏域
html += "<input type='hidden' name='id' value='"+contact.getId()+"'/>";
html += "<table align='center' border='1' width='300px'>";
html += " <tr>";
html += " <th>姓名</th>";
html += " <td><input type='text' name='name' value='"+contact.getName()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>性别</th>";
html += " <td>"; if(contact.getGender().equals("男")){
html += " <input type='radio' name='gender' value='男' checked='checked'/>男";
html += " <input type='radio' name='gender' value='女'/>女";
}else if(contact.getGender().equals("女")){
html += " <input type='radio' name='gender' value='男'/>男";
html += " <input type='radio' name='gender' value='女' checked='checked'/>女";
}else{
html += " <input type='radio' name='gender' value='男' checked='checked'/>男";
html += " <input type='radio' name='gender' value='女'/>女";
} html += " </td>";
html += " </tr>";
html += " <tr>";
html += " <th>年龄</th>";
html += " <td><input type='text' name='age' value='"+contact.getAge()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>电话</th>";
html += " <td><input type='text' name='phone' value='"+contact.getPhone()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>邮箱</th>";
html += " <td><input type='text' name='email' value='"+contact.getEmail()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>QQ</th>";
html += " <td><input type='text' name='qq' value='"+contact.getQq()+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <td colspan='2' align='center'>";
html += " <input type='submit' value='保存'/>&nbsp;";
html += " <input type='reset' value='重置'/></td>";
html += " </tr>";
html += "</table>";
html += "</form>";
html += "</body>";
html += "</html>"; writer.write(html);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.4修改联系人的逻辑

 public class UpdateContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.接收参数
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String qq = request.getParameter("qq"); //封装成Contact对象
Contact contact = new Contact();
contact.setId(id);
contact.setName(name);
contact.setGender(gender);
contact.setAge(Integer.parseInt(age));
contact.setPhone(phone);
contact.setEmail(email);
contact.setQq(qq); //2.调用dao修改联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.updateContact(contact); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

3.5删除联系人的逻辑

 public class DeleteContactServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//在火狐浏览器中以Get方式提交带参数的数据,会重复提交两次。
System.out.println("删除联系人");
//1.接收id
String id = request.getParameter("id"); //2.调用dao删除联系人的方法
ContactDao dao = new ContactDaoImpl();
dao.deleteContact(id); //3.跳转到查询联系人的页面
response.sendRedirect(request.getContextPath()+"/ListContactServlet");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

4 功能测试(测试攻城狮)

5 性能测试(测试攻城狮)

6 部署上线(实施攻城狮)

7 维护阶段(实施攻城狮)

<项目><day12>通讯录(视频)的更多相关文章

  1. <项目><day12>通讯录(自己做的)

    设计一个通讯录主页面 <!DOCTYPE html> <html> <head> <title>电话本首页</title> <meta ...

  2. Asp.NET Core2.0 项目实战入门视频课程_完整版

    END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...

  3. 给tomcat配置外部资源路径(应用场景:web项目访问图片视频等资源)

    对于一个web项目来说,除了文字之外,图片,视频等媒体元素也是其重要的组成部分.我们知道,web项目中如果用到大量的图片.视屏的资源,我们 通常的做法是只在数据库中存储图片.视频等资源的路径,web项 ...

  4. 0506-Scrum 项目 2.0视频

    一.团队项目要求 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 二.NABCD模型 选题:约拍平台——家教平台 1) ...

  5. 优秀开源项目之一:视频监控系统iSpy

    iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...

  6. 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)

    在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...

  7. 实战项目:通讯录&nbsp;UI—第十一天

     1.推出视图的两种方式:  1.通过导航控制器push到下一个界面,使用pop返回到上一个界面 2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismi ...

  8. python项目开发视频

    精品Python项目开发学习视频 所属网站分类: 资源下载 > python视频教程 作者:乐天派 链接:http://www.pythonheidong.com/blog/article/44 ...

  9. 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享

    项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...

随机推荐

  1. codevs 3070 寻找somebody4(水题日常)

     时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 有一天.....sb不见了,有个人要去找他..他发现sb在一个杨辉三角里.. ...

  2. TebsorFlow低阶API(五)—— 保存和恢复

    简介 tf.train.Saver 类提供了保存和恢复模型的方法.通过 tf.saved_model.simple_save 函数可以轻松地保存适合投入使用的模型.Estimator会自动保存和恢复 ...

  3. QT_8_Qt中的事件处理_定时器事件_定时器类_事件分发器_事件过滤器_绘图事件_高级绘图事件_绘图设备_QFile 文件读写_QFileInfo文件信息

    Qt中的事件处理 1.1. 捕获QLabel中是鼠标事件 1.2. enterevent 鼠标进入 1.3. leaveevent 鼠标离开 1.4. 鼠标按下MyLabel::mousePressE ...

  4. RestTemplate-postForObject源码

    参数:    请求路径, 请求参数, 返回类型, 扩展模板变量

  5. Oracle中的DDL,DML,DCL总结

    转自http://blog.csdn.net/w183705952/article/details/7354974 DML(Data Manipulation Language,数据操作语言):用于检 ...

  6. Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载

    crud指数据库或者持久层的基本操作,包括 增加(Create).读取查询(Retrieve 取回).更新(Update)和删除(Delete) Spring不仅对JDBC进行了封装,也对Hibern ...

  7. Xcode导入第三方库

    Xcode导入第三方库,例如TapkuLibrary iOS开源框架Tapku下载地址:https://github.com/devinross/tapkulibrary.git 1.创建你的工程项目 ...

  8. composer 插件安装

    https://packagist.org/?q=phpmyadmin&p=0 Github:笔记 https://github.com/13431/php 类库包下载地址:packagist ...

  9. Cookie 详解以及实现一个 cookie 操作库

    Cookie 详解以及实现一个 cookie 操作库 cookie 在前端有着大量的应用,但有时我们对它还是一知半解.下面来看看它的一些具体的用法 Set-Cookie 服务器通过设置响应头来设置客户 ...

  10. Python3.6中文文档 又来推荐一个,之前的Python3.52看得有点懵逼 https://www.rddoc.com/doc/Python/3.6.0/zh/

    https://www.rddoc.com/doc/Python/3.6.0/zh/    大家有空看下