将之前用servlet写的程序转化为jsp+servlet的简单的MVC的三层结构。项目中程序的包如图


首先是实体对象:

  1. package com.contactSystem.entiey;
  2.  
  3. public class Contact {
  4. private String Id;
  5. private String name;
  6. private String sex;
  7. private String age;
  8. private String phone;
  9. private String qq;
  10. private String email;
  11. public String getId() {
  12. return Id;
  13. }
  14. public void setId(String id) {
  15. Id = id;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public String getSex() {
  24. return sex;
  25. }
  26. public void setSex(String sex) {
  27. this.sex = sex;
  28. }
  29. public String getAge() {
  30. return age;
  31. }
  32. public void setAge(String age) {
  33. this.age = age;
  34. }
  35. public String getPhone() {
  36. return phone;
  37. }
  38. public void setPhone(String phone) {
  39. this.phone = phone;
  40. }
  41. public String getQq() {
  42. return qq;
  43. }
  44. public void setQq(String qq) {
  45. this.qq = qq;
  46. }
  47. public String getEmail() {
  48. return email;
  49. }
  50. public void setEmail(String email) {
  51. this.email = email;
  52. }
  53. @Override
  54. public String toString() {
  55. return "Contact [Id=" + Id + ", name=" + name + ", sex=" + sex
  56. + ", age=" + age + ", phone=" + phone + ", qq=" + qq
  57. + ", email=" + email + "]";
  58. }
  59.  
  60. }

然后就是对数据操作的抽象类

  1. package com.contactSystem.dao;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.util.List;
  6.  
  7. import org.dom4j.DocumentException;
  8. import com.contactSystem.entiey.Contact;
  9.  
  10. public interface ContactOperate {
  11. public void addContact(Contact contact) throws Exception;
  12. public void updateContact(Contact contact) throws Exception;
  13. public void removeContact(String id) throws Exception;
  14. public Contact findContact(String id) throws Exception;
  15. public List<Contact> allContacts();
  16. //根据名称姓名查询是否有存在重复。
  17. public boolean checkIfContact(String name);
  18. }

具体实现类

  1. package com.contactSystem.dao.daoImpl;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.UUID;
  7.  
  8. import javax.persistence.Id;
  9. import javax.persistence.Tuple;
  10.  
  11. import org.dom4j.Document;
  12. import org.dom4j.DocumentHelper;
  13. import org.dom4j.Element;
  14. import org.dom4j.io.SAXReader;
  15.  
  16. import com.contactSystem.dao.ContactOperate;
  17. import com.contactSystem.entiey.Contact;
  18. import com.contactSystem.util.XMLUtil;
  19.  
  20. public class Operater implements ContactOperate {
  21.  
  22. @Override
  23. //添加联系人
  24. public void addContact(Contact contact) throws Exception {
  25. // TODO Auto-generated method stub
  26. File file=new File("e:/contact.xml");
  27. Document doc=null;
  28. Element rootElem=null;
  29. if (file.exists()) {
  30. doc=new SAXReader().read(file);
  31. rootElem=doc.getRootElement();
  32. }else {
  33. doc=DocumentHelper.createDocument();
  34. rootElem=doc.addElement("ContactList");
  35. }
  36.  
  37. //开始添加个体
  38. Element element=rootElem.addElement("contact");
  39. //有系统自动生成一随机且唯一的ID,赋给联系人Id,系统提供了一个包UUID包
  40. String uuid=UUID.randomUUID().toString().replace("-", "");
  41.  
  42. element.addAttribute("Id", uuid);
  43. element.addElement("姓名").setText(contact.getName());
  44. element.addElement("name").setText(contact.getName());
  45. element.addElement("sex").setText(contact.getSex());
  46. element.addElement("age").setText(contact.getAge());
  47. element.addElement("phone").setText(contact.getPhone());
  48. element.addElement("email").setText(contact.getEmail());
  49. element.addElement("qq").setText(contact.getQq());
  50.  
  51. //写入到本地的xml文档中
  52. XMLUtil.write2xml(doc);
  53.  
  54. }
  55.  
  56. @Override
  57. public void updateContact(Contact contact) throws Exception {
  58. // TODO Auto-generated method stub
  59. //通过xpath查找对应id的联系人
  60. Document document=XMLUtil.getDocument();
  61. Element element=(Element) document.selectSingleNode("//contact[@Id='"+contact.getId()+"']");
  62. //根据标签改文本
  63. System.out.println(element);
  64. element.element("name").setText(contact.getName());
  65. element.element("age").setText(contact.getAge());
  66. element.element("email").setText(contact.getEmail());
  67. element.element("phone").setText(contact.getPhone());
  68. element.element("sex").setText(contact.getSex());
  69. element.element("qq").setText(contact.getQq());
  70. XMLUtil.write2xml(document);
  71.  
  72. }
  73.  
  74. @Override
  75. public void removeContact(String id) throws Exception {
  76. // TODO Auto-generated method stub
  77. //通过xpath去查找对应的contact
  78. Document document=XMLUtil.getDocument();
  79. Element element=(Element) document.selectSingleNode("//contact[@Id='"+id+"']");
  80. /**
  81. * 如果是火狐浏览器,其本身有一个bug,对于一个get请求,它会重复做两次,
  82. * 第一次会把一个唯一的id给删除掉,但是在第二次的时候,它会继续去删,而这个时候查找不到,会报错,会发生错误,
  83. * 为了解决这个bug,我们在这里验证其是否为空
  84. */
  85. if (element!=null) {
  86. element.detach();
  87. XMLUtil.write2xml(document);
  88. }
  89. }
  90.  
  91. @Override
  92. public Contact findContact(String id) throws Exception {
  93. // TODO Auto-generated method stub
  94. Document document=XMLUtil.getDocument();
  95. Element e=(Element) document.selectSingleNode("//contact[@Id='"+id+"']");
  96.  
  97. Contact contact=null;
  98. if (e!=null) {
  99. contact=new Contact();
  100. contact.setAge(e.elementText("age"));
  101. contact.setEmail(e.elementText("email"));
  102. contact.setId(id);
  103. contact.setName(e.elementText("name"));
  104. contact.setPhone(e.elementText("phone"));
  105. contact.setSex(e.elementText("sex"));
  106. contact.setQq(e.elementText("qq"));
  107. }
  108. return contact;
  109. }
  110.  
  111. @Override
  112. public List<Contact> allContacts() {
  113. // TODO Auto-generated method stub
  114. Document document=XMLUtil.getDocument();
  115. List<Contact> list=new ArrayList<Contact>();
  116. List<Element> conElements=(List<Element>)document.selectNodes("//contact");
  117. for (Element element : conElements) {
  118. Contact contact=new Contact();
  119. contact.setId(element.attributeValue("Id"));
  120. contact.setAge(element.elementText("age"));
  121. contact.setEmail(element.elementText("email"));
  122. contact.setName(element.elementText("name"));
  123. contact.setPhone(element.elementText("phone"));
  124. contact.setQq(element.elementText("qq"));
  125. contact.setSex(element.elementText("sex"));
  126. list.add(contact);
  127. }
  128. return list;
  129. }
  130. /**
  131. * true:说明重复
  132. * false:说明没有重复
  133. */
  134. @Override
  135. public boolean checkIfContact(String name) {
  136. // TODO Auto-generated method stub
  137. //查询name标签是否一样
  138. Document doc=XMLUtil.getDocument();
  139. Element element=(Element) doc.selectSingleNode("//name[text()='"+name+"']");
  140. if (element!=null) {
  141. return true;
  142. }else {
  143. return false;
  144. }
  145.  
  146. }
  147.  
  148. }

为了减轻Servlet的负担在增加一层(业务逻辑层)Service,这里举例,当联系人的名字存在时,提示出错,不在servlet中去判断,而在service中去处理,同样首先写出service抽象接口

  1. package com.contactSystem.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.contactSystem.entiey.Contact;
  6.  
  7. public interface ContactService {
  8. public void addContact(Contact contact) throws Exception;
  9. public void updateContact(Contact contact) throws Exception;
  10. public void removeContact(String id) throws Exception;
  11. public Contact findContact(String id) throws Exception;
  12. public List<Contact> allContacts();
  13. }

这个时候有点相当于在Operate操作中添加了一层操作,在contactService的实现类中去有逻辑判断的去使用Operater的方法

  1. package com.contactSystem.service.imple;
  2.  
  3. import java.util.List;
  4.  
  5. import com.contactSystem.dao.daoImpl.Operater;
  6. import com.contactSystem.entiey.Contact;
  7. import com.contactSystem.exception.NameRepeatException;
  8. import com.contactSystem.service.ContactService;
  9. /**
  10. * 处理项目中出现的业务逻辑
  11. * @author Administrator
  12. *
  13. */
  14. public class ContactServiceImpe implements ContactService {
  15. Operater operater =new Operater();
  16. @Override
  17. public void addContact(Contact contact) throws Exception {
  18. // TODO Auto-generated method stub
  19. //执行业务逻辑判断
  20. /**
  21. * 添加是否重复的业务逻辑
  22. */
  23. if (operater.checkIfContact(contact.getName())) {
  24. //重复
  25. /**
  26. * 注意:如果业务层方法出现任何错误,则返回标记(自定义的异常)到servlet
  27. */
  28. throw new NameRepeatException("姓名重复,不可使用");
  29. }
  30. operater.addContact(contact);
  31.  
  32. }
  33.  
  34. @Override
  35. public void updateContact(Contact contact) throws Exception {
  36. // TODO Auto-generated method stub
  37. operater.updateContact(contact);
  38. }
  39.  
  40. @Override
  41. public void removeContact(String id) throws Exception {
  42. // TODO Auto-generated method stub
  43. operater.removeContact(id);
  44. }
  45.  
  46. @Override
  47. public Contact findContact(String id) throws Exception {
  48. // TODO Auto-generated method stub
  49. return operater.findContact(id);
  50. }
  51.  
  52. @Override
  53. public List<Contact> allContacts() {
  54. // TODO Auto-generated method stub
  55. return operater.allContacts();
  56. }
  57.  
  58. }

这里通过抛出异常去将信息传递给Servlet,异常代码如下

  1. package com.contactSystem.exception;
  2. /**
  3. * 姓名重复的自定义异常
  4. * @author Administrator
  5. *
  6. */
  7. public class NameRepeatException extends Exception{
  8. public NameRepeatException(String msg) {
  9. super(msg);
  10. }
  11. }

现在来写Servlet,首先是首页的联系人列表Servlet

  1. package com.contactSystem.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import com.contactSystem.dao.daoImpl.Operater;
  14. import com.contactSystem.entiey.Contact;
  15. import com.contactSystem.service.ContactService;
  16. import com.contactSystem.service.imple.ContactServiceImpe;
  17.  
  18. public class Index extends HttpServlet {
  19.  
  20. /**
  21. * 显示所有联系人的逻辑方式
  22. */
  23. private static final long serialVersionUID = 1L;
  24.  
  25. public void doGet(HttpServletRequest request, HttpServletResponse response)
  26. throws ServletException, IOException {
  27. response.setContentType("text/html;charset=utf-8");
  28. ContactService service=new ContactServiceImpe();
  29. List<Contact> contacts=service.allContacts();
  30.  
  31. /**
  32. * shift+alt+A ——>区域选择
  33. * 正则表达式:“."表示任意字符,"*"表示多个字符
  34. * “/1”表示一行代表匹配一行内容
  35. */
  36.  
  37. request.setAttribute("contacts", contacts);
  38. request.getRequestDispatcher("/index.jsp").forward(request, response);
  39.  
  40. }
  41.  
  42. public void doPost(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. this.doGet(request, response);
  45. }
  46.  
  47. }

添加联系人

  1. package com.contactSystem.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.contactSystem.dao.daoImpl.Operater;
  12. import com.contactSystem.entiey.Contact;
  13. import com.contactSystem.exception.NameRepeatException;
  14. import com.contactSystem.service.ContactService;
  15. import com.contactSystem.service.imple.ContactServiceImpe;
  16.  
  17. public class addServlet extends HttpServlet {
  18.  
  19. /**
  20. * 处理添加联系人的逻辑
  21. */
  22. private static final long serialVersionUID = 1L;
  23.  
  24. public void doGet(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException {
  26. request.setCharacterEncoding("utf-8");
  27. String userName=request.getParameter("userName");
  28. String age=request.getParameter("age");
  29. String sex=request.getParameter("sex");
  30. String phone=request.getParameter("phone");
  31. String qq=request.getParameter("qq");
  32. String email=request.getParameter("email");
  33.  
  34. ContactService service=new ContactServiceImpe();
  35.  
  36. Contact contact=new Contact();
  37. contact.setName(userName);
  38. contact.setAge(age);
  39. contact.setSex(sex);
  40. contact.setPhone(phone);
  41. contact.setQq(qq);
  42. contact.setEmail(email);
  43. try {
  44. service.addContact(contact);
  45. } catch (NameRepeatException e) {
  46. // TODO Auto-generated catch block
  47. //处理名字重复的异常
  48. request.setAttribute("message", e.getMessage());
  49. request.getRequestDispatcher("/add.jsp").forward(request, response);
  50. return;
  51. } catch (Exception e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55.  
  56. response.sendRedirect(request.getContextPath()+"/Index");
  57. }
  58.  
  59. public void doPost(HttpServletRequest request, HttpServletResponse response)
  60. throws ServletException, IOException {
  61. this.doGet(request, response);
  62. }
  63.  
  64. }

查找联系人

  1. package com.contactSystem.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.contactSystem.dao.daoImpl.Operater;
  12. import com.contactSystem.entiey.Contact;
  13. import com.contactSystem.service.ContactService;
  14. import com.contactSystem.service.imple.ContactServiceImpe;
  15.  
  16. public class FindIdServlet extends HttpServlet {
  17.  
  18. /**
  19. * 修改联系人逻辑
  20. */
  21. private static final long serialVersionUID = 1L;
  22.  
  23. public void doGet(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException {
  25.  
  26. response.setContentType("text/html;charset=utf-8");
  27.  
  28. ContactService service=new ContactServiceImpe();
  29.  
  30. String id=request.getParameter("id");
  31.  
  32. Contact contact=null;
  33. try {
  34. contact=service.findContact(id);
  35. } catch (Exception e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39.  
  40. request.setAttribute("contact", contact);
  41. request.getRequestDispatcher("/update.jsp").forward(request, response);
  42. }
  43.  
  44. public void doPost(HttpServletRequest request, HttpServletResponse response)
  45. throws ServletException, IOException {
  46. this.doGet(request, response);
  47. }
  48.  
  49. }

修改联系人

  1. package com.contactSystem.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.contactSystem.dao.daoImpl.Operater;
  12. import com.contactSystem.entiey.Contact;
  13. import com.contactSystem.service.ContactService;
  14. import com.contactSystem.service.imple.ContactServiceImpe;
  15.  
  16. public class UpdateServlet extends HttpServlet {
  17.  
  18. /**
  19. * 将修改后的数据提交
  20. */
  21. private static final long serialVersionUID = 1L;
  22.  
  23. public void doGet(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException {
  25.  
  26. response.setContentType("text/html");
  27.  
  28. request.setCharacterEncoding("utf-8");
  29. String userName=request.getParameter("userName");
  30. String age=request.getParameter("age");
  31. String sex=request.getParameter("sex");
  32. String phone=request.getParameter("phone");
  33. String qq=request.getParameter("qq");
  34. String email=request.getParameter("email");
  35. String id=request.getParameter("id");
  36. ContactService service=new ContactServiceImpe();
  37. Contact contact=new Contact();
  38. contact.setId(id);
  39. contact.setName(userName);
  40. contact.setAge(age);
  41. contact.setSex(sex);
  42. contact.setPhone(phone);
  43. contact.setQq(qq);
  44. contact.setEmail(email);
  45. try {
  46. service.updateContact(contact);
  47. } catch (Exception e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51.  
  52. response.sendRedirect(request.getContextPath()+"/Index");
  53.  
  54. }
  55.  
  56. public void doPost(HttpServletRequest request, HttpServletResponse response)
  57. throws ServletException, IOException {
  58. this.doGet(request, response);
  59. }
  60.  
  61. }

删除联系人

  1. package com.contactSystem.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import com.contactSystem.dao.daoImpl.Operater;
  12. import com.contactSystem.service.ContactService;
  13. import com.contactSystem.service.imple.ContactServiceImpe;
  14.  
  15. public class DeleteServlet extends HttpServlet {
  16.  
  17. public void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19.  
  20. response.setContentType("text/html;charset=utf-8");
  21.  
  22. String id=request.getParameter("id");
  23. ContactService service=new ContactServiceImpe();
  24. try {
  25. service.removeContact(id);
  26. } catch (Exception e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. }
  30.  
  31. response.sendRedirect(request.getContextPath()+"/Index");
  32.  
  33. }
  34.  
  35. public void doPost(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37. this.doGet(request, response);
  38. }
  39. }

以上完成了MVC的M和C,还差View,现在来具体显示jsp页面

首页所有联系人的显示

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <!DOCTYPE html>
  4. <html>
  5.  
  6. <head>
  7. <meta charset='utf-8'>
  8. <title>查询所有联系人</title>
  9. <style media='screen'>
  10. table td {
  11. text-align: center;
  12. }
  13.  
  14. table {
  15. border-collapse: collapse;
  16. }
  17. </style>
  18. </head>
  19.  
  20. <body>
  21. <center>
  22. <h2>查询所有联系人</h2>
  23. </center>
  24. <table border='1' align='center'>
  25. <tbody>
  26. <thead>
  27. <th>编号</th>
  28. <th>姓名</th>
  29. <th>性别</th>
  30. <th>年龄</th>
  31. <th>电话</th>
  32. <th>QQ</th>
  33. <th>邮箱</th>
  34. <th>操作</th>
  35. </thead>
  36. <c:forEach items="${contacts}" var="con" varStatus="varSta">
  37. <tr>
  38. <td>${varSta.count }</td>
  39. <td>${con.name }</td>
  40. <td>${con.sex }</td>
  41. <td>${con.age }</td>
  42. <td>${con.phone }</td>
  43. <td>${con.qq }</td>
  44. <td>${con.email }</td>
  45. <td><a href='${pageContext.request.contextPath }/FindIdServlet?id=${con.id}'>修改</a>  <a href='${pageContext.request.contextPath }/DeleteServlet?id=${con.id}'>删除</a></td>
  46. </tr>
  47. </c:forEach>
  48. <tr>
  49. <td colspan='8'>
  50. <a href='${pageContext.request.contextPath}/add.jsp'>添加联系人</a>
  51. </td>
  52. </tr>
  53. </tbody>
  54. </table>
  55. </body>
  56.  
  57. </html>

  

修改联系人,首先要把要修改联系人的信息都显示在信息栏中,然后用户去修改相关信息

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <!DOCTYPE html>
  4. <html>
  5.  
  6. <head>
  7. <meta charset="utf-8">
  8. <title>修改联系人</title>
  9. <style media="screen">
  10. #btn{
  11. width:40px;
  12. width: 50px;
  13. background: green;
  14. color: white;
  15. font-size:14px;
  16. }
  17. </style>
  18. </head>
  19.  
  20. <body>
  21. <center>
  22. <h2>修改联系人</h2>
  23. </center>
  24. <form action="${pageContext.request.contextPath }/UpdateServlet" method="post">
  25. <table border="1" align="center">
  26. <tbody>
  27.  
  28. <tr>
  29. <th>姓名</th>
  30. <td><input type="text" name="userName" value="${contact.name}"/></td>
  31. <input type="hidden" name="id" value="${contact.id }"/>
  32.  
  33. </tr>
  34. <tr>
  35. <th>年龄</th>
  36. <td><input type="text" name="age" value="${contact.age }" /></td>
  37. </tr>
  38. <tr>
  39. <th>性别</th>
  40. <td>
  41.  
  42. <input type="radio" name="sex" value="男" <c:if test="${contact.sex=='男' }"> checked="true"</c:if>/>男  
  43. <input type="radio" name="sex" value="女" <c:if test="${contact.sex=='女' }"> checked="true"</c:if> />女
  44. </td>
  45. </tr>
  46. <tr>
  47. <th>电话</th>
  48. <td><input type="text" name="phone" value="${contact.phone }" /></td>
  49. </tr>
  50. <tr>
  51. <th>QQ</th>
  52. <td><input type="text" name="qq" value="${contact.qq }" /></td>
  53. </tr>
  54. <tr>
  55. <th>邮箱</th>
  56. <td><input type="text" name="email" value="${contact.email }" /></td>
  57. </tr>
  58. <tr>
  59. <td colspan="3" align="center">
  60. <input type="submit" value="提交" id="btn"/>
  61. </td>
  62. </tr>
  63. </tbody>
  64. </table>
  65. </form>
  66. </body>
  67.  
  68. </html>

  

最后就是添加联系人了

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <!DOCTYPE html>
  4. <html>
  5.  
  6. <head>
  7. <meta charset="utf-8">
  8. <title>添加联系人</title>
  9. <style media="screen">
  10. #btn{
  11. width:40px;
  12. width: 50px;
  13. background: green;
  14. color: white;
  15. font-size:14px;
  16. }
  17. </style>
  18. </head>
  19.  
  20. <body>
  21. <center>
  22. <h2>添加联系人</h2>
  23. </center>
  24. <form action="${pageContext.request.contextPath}/addServlet" method="post">
  25. <table border="1" align="center">
  26. <tbody>
  27.  
  28. <tr>
  29. <th>姓名</th>
  30. <td><input type="text" name="userName" value="${message }" /></td>
  31. </tr>
  32. <tr>
  33. <th>年龄</th>
  34. <td><input type="text" name="age" /></td>
  35. </tr>
  36. <tr>
  37. <th>性别</th>
  38. <td>
  39. <input type="radio" name="sex" value="男"/>男  
  40. <input type="radio" name="sex" value="女" />
  41. </td>
  42. </tr>
  43. <tr>
  44. <th>电话</th>
  45. <td><input type="text" name="phone" /></td>
  46. </tr>
  47. <tr>
  48. <th>QQ</th>
  49. <td><input type="text" name="qq" /></td>
  50. </tr>
  51. <tr>
  52. <th>邮箱</th>
  53. <td><input type="text" name="email" /></td>
  54. </tr>
  55. <tr>
  56. <td colspan="3" align="center">
  57. <input type="submit" value="提交" id="btn"/>
  58. </td>
  59. </tr>
  60. </tbody>
  61. </table>
  62. </form>
  63. </body>
  64.  
  65. </html>

最后可以加上一个测试类,方便自己调试

  1. package com.contactSystem.Junit;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.util.List;
  6.  
  7. import org.junit.Before;
  8.  
  9. import com.contactSystem.dao.daoImpl.Operater;
  10. import com.contactSystem.entiey.Contact;
  11.  
  12. public class Test {
  13. Operater operator=null;
  14.  
  15. //初始化这个对象的实例
  16. @Before
  17. public void init(){
  18. operator=new Operater();
  19. }
  20.  
  21. @org.junit.Test
  22. public void Add() throws Exception{
  23. Contact contact=new Contact();
  24.  
  25. contact.setAge("21");
  26. contact.setEmail("454444@qq.com");
  27. contact.setName("gqxing");
  28. contact.setPhone("13455555");
  29. contact.setQq("235346662");
  30. contact.setSex("男");
  31. operator.addContact(contact);
  32. }
  33.  
  34. @org.junit.Test
  35. public void update() throws Exception{
  36. Contact contact=new Contact();
  37. contact.setId("002");
  38. contact.setAge("0");
  39. contact.setEmail("0000000@qq.com");
  40. contact.setName("test");
  41. contact.setPhone("0-00000000000");
  42. contact.setQq("000000000000");
  43. contact.setSex("男");
  44. operator.updateContact(contact);
  45. }
  46.  
  47. @org.junit.Test
  48. public void removeContact() throws Exception{
  49. operator.removeContact("002");
  50. }
  51.  
  52. @org.junit.Test
  53. public void findContact() throws Exception{
  54. Contact contact=operator.findContact("002");
  55. System.out.println(contact);
  56. }
  57.  
  58. @org.junit.Test
  59. public void allContact() throws Exception{
  60. List<Contact> contacts= operator.allContacts();
  61. for (Contact contact : contacts) {
  62. System.out.println(contact);
  63. }
  64. }
  65. @org.junit.Test
  66. public void TetsCheckBoolean() throws Exception{
  67. Boolean flag=operator.checkIfContact("郭庆兴");
  68. System.out.println(flag);
  69. }
  70.  
  71. }

最后运行的结构示意图:

当名字重复的时候:提交就会显示

通讯录改造——MVC设计模式的更多相关文章

  1. AngularJS_01之基础概述、设计原则及MVC设计模式

    1.AngularJS: 开源的JS框架,用来开发单一页面应用,以及数据操作频繁的场景:2.设计原则: ①YAGNI原则:You Aren't Gonna Need It! 不要写不需要的代码! ②K ...

  2. 谈谈JAVA工程狮面试中经常遇到的面试题目------什么是MVC设计模式

    作为一名java工程狮,大家肯定经历过很多面试,但每次几乎都会被问到什么是MVC设计模式,你是怎么理解MVC的类似这样的一系列关于MVC的问题. [出现频率] [关键考点] MVC的含义 MVC的结构 ...

  3. Java Web开发中MVC设计模式简介

    一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条 ...

  4. MVC设计模式与三层架构

    三层架构分别是:表示层(Web层).业务逻辑层(BLL层)和数据访问层(DAL层). (1)表示层负责: a.从用户端收集信息 b.将用户信息发送到业务服务层做处理 c.从业务服务层接收处理结果 d. ...

  5. 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销

    第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...

  6. mvc设计模式和mvc框架的区别

    Spring中的新名称也太多了吧!IOC/DI/MVC/AOP/DAO/ORM... 对于刚刚接触spring的我来说确实晕了头!可是一但你完全掌握了一个概念,那么它就会死心塌地的为你服务了.这可比女 ...

  7. MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

    设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...

  8. MVC设计模式(持续更新中)

    MVC设计模式--->英文全称为: model(模型)  View (视图)  Controller(控制)   MVC是一种设计思想.这种思想强调实现模型(Model).视图(View)和控制 ...

  9. iOS中MVC设计模式

    在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...

随机推荐

  1. osg学习笔记3 简单几何模型

    osg::Geode (geometry node) osg::Geode类表示场景中的渲染几何叶节点,它包含了渲染用的几何信息,没有子节点. 要绘制的几何数据保存在osg::Geode管理的一组os ...

  2. 数据库 - FMDB

    FMDB 是基于 SQLite 封装的 面向对对象(OC) 的API. FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API FMDB 需要libsqli ...

  3. IOS7,做为开发者,你需要知道的变更

    IOS7即将发布,那么我们需要做些什么呢? 升级你的程序Icon至 120*120 更新一张包含状态栏大小的闪屏图片 还有些什么东西呢? IOS7中需要使用更加扁平化的设计,所以BUTTON的图片,边 ...

  4. 第 17 章 责任链模式【Chain of Responsibility Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 中国古代对妇女制定了“三从四德”的道德规范,“三从”是指“未嫁从父.既嫁从夫.夫死从子”,也就是说一个女性,在没有结婚的 ...

  5. Java中double类型的数据精确到小数点后两位

    Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...

  6. jquery多级手风琴插件–accordion.js

    手风琴菜单一般用于下拉导航,由于外观非常简洁,使用起来跟手风琴一样可以拉伸和收缩而得名,项目中适当应用手风琴效果会给用户带来非常好的体验.本文借助jQuery插件轻松打造一个非常不错的手风琴效果的菜单 ...

  7. Hard Life

    poj3155:http://poj.org/problem?id=3155 题意:最大密度子图的模板题. 题解:直接看代码. /* 题意简述一个公司有n个人,给出了一些有冲突的人的对数(u,v),所 ...

  8. 《鸟哥的Linux私房菜》读书笔记三

    1.在Linux系统中,每个设备都被当成一个文件来对待,每个设备都会有设备文件名.比如 IDE接口的硬盘文件名为/dev/hd[a-d] 括号内的字母为a-d当中任意一个,即/dev/hda,/dev ...

  9. BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...

  10. WordPress Cart66 Lite插件跨站请求伪造漏洞

    漏洞名称: WordPress Cart66 Lite插件跨站请求伪造漏洞 CNNVD编号: CNNVD-201310-524 发布时间: 2013-10-23 更新时间: 2013-10-23 危害 ...