<项目><day12>通讯录(视频)
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> <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='保存'/> ";
- 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>通讯录(视频)的更多相关文章
- <项目><day12>通讯录(自己做的)
设计一个通讯录主页面 <!DOCTYPE html> <html> <head> <title>电话本首页</title> <meta ...
- Asp.NET Core2.0 项目实战入门视频课程_完整版
END OR START? 看到这个标题,你开不开心,激不激动呢? 没错,.net core的入门课程已经完毕了.52ABP.School项目从11月19日,第一章视频的试录制,到今天完整版出炉,离不 ...
- 给tomcat配置外部资源路径(应用场景:web项目访问图片视频等资源)
对于一个web项目来说,除了文字之外,图片,视频等媒体元素也是其重要的组成部分.我们知道,web项目中如果用到大量的图片.视屏的资源,我们 通常的做法是只在数据库中存储图片.视频等资源的路径,web项 ...
- 0506-Scrum 项目 2.0视频
一.团队项目要求 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 二.NABCD模型 选题:约拍平台——家教平台 1) ...
- 优秀开源项目之一:视频监控系统iSpy
iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...
- 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)
在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...
- 实战项目:通讯录 UI—第十一天
1.推出视图的两种方式: 1.通过导航控制器push到下一个界面,使用pop返回到上一个界面 2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismi ...
- python项目开发视频
精品Python项目开发学习视频 所属网站分类: 资源下载 > python视频教程 作者:乐天派 链接:http://www.pythonheidong.com/blog/article/44 ...
- 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享
项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...
随机推荐
- 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复
源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...
- learnpythonthehardway EX41 相关
str.count() # str.count()方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. # str.count(sub, start= 0,end=len( ...
- (转)配置Spring管理的bean的作用域
http://blog.csdn.net/yerenyuan_pku/article/details/52833477 Spring管理的bean的作用域有: singleton 在每个Spring ...
- 如何修改站点url
1.config目录下的config_global.php 文件,修改:$_config['cookie']['cookiedomain'] = '.xxxxx.com';2.config目录下的co ...
- DROP SCHEMA - 删除一个模式
SYNOPSIS DROP SCHEMA name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP SCHEMA 从数据库中删除模式. 模式只能被 ...
- QT+信号和槽函数_自定义槽函数_一个信号对应多个槽函数
以下的代码里面有自定义槽函数的内容,同时也有信号实现的函数: #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #i ...
- JavaScript 非常重要的几个概念
JavaScript是一门比较复杂的语言.如果你是一名JavaScript开发人员,不管处于什么样的水平,都有必要了解JavaScript的基本概念.小编最近的工作涉及到JavaScript,于是本文 ...
- 【转载】WampServer图标显示红色后变成橙色怎么解决
WampServer就是Windows Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件. 工具/原料 WampServer 方法/步 ...
- 树莓派 - MQTT
安装mosquitto 下载源代码包 wget http://mosquitto.org/files/source/mosquitto-1.5.tar.gz 解压 tar zxfv mosquitto ...
- CSS小知识点一
1. text-indent属性 缩进文本 通过使用 text-indent 属性,所有元素的第一行都可以缩进一个给定的长度,甚至该长度可以是负值.这个属性最常见的用途是将段落的首行缩进,一 ...