JavaWeb:基于MVC设计模式的一个小案例(二)

今天是把昨天没有写完的功能补充完整。

在CustomerServlet.java 中,增加了添加信息的功能,补上代码如下:

 1     private void addCustomer(HttpServletRequest request, HttpServletResponse response) 
 2             throws ServletException, IOException {
 3         
 4         //1、获取表单信息:name、address、phone
 5         String name = request.getParameter("name");
 6         String address = request.getParameter("address");
 7         String phone = request.getParameter("phone");
 8         
 9         //2、检验 name 是否被占用
10         //2.1、调用 CustomerDao 的 getCountWithName(String name)方法,获取name 在数据库中是否存在
11         long count = customerDao.getCountWithName(name);
12         //2.2、若返回值大于 0 , 则响应 newCustomer.jsp 页面:通过转发的方式
13         if(count > 0){
14             //2.2.1、要求在 newCustomer.jsp 页面显示一个错误提示:用户名 name 已经被占用,请重新选择。
15             //在 request 中放入一个属性 message:用户名 name 已经被占用,请重新选择。
16             //在 newcustomer.jsp 页面上通过 request.getAttribute("message") 的方式来显示。
17             request.setAttribute("message", "用户名" + name + "已经被占用,请重新选择");
18             
19             //2.2.2、newCustomer.jsp 的表单可以回显。
20             //通过 value=<%= request.getParameter("name") == null ? "" : request.getParameter("name")%> 
21             //来进行回显。
22             
23             request.getRequestDispatcher("/newCustomer.jsp").forward(request, response);
24             //2.2.3、结束方法 return 
25             return;
26         }
27 
28         //3、把表单参数封装为一个 Customer 对象 customer
29         Customer customer = new Customer(name, address, phone);
30         //4、调用 CustomerDao 的 save 方法,执行保存操作
31         customerDao.save(customer);
32         //5.重定向到 success.jsp 页面。要是用转发,刷新页面会有表单的重复提交。
33         response.sendRedirect("success.jsp");
34     }

然后我是创建了 newcustomer.jsp,代码如下:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%
11         Object mes = request.getAttribute("message");
12         if(mes != null){
13     %>
14             <br>
15             <font color="red"><%= mes %></font>
16     <% 
17         }
18     %>
19     
20     <form action="addCustomer.do" method="post">
21         <table>
22             <tr>
23                 <td>Name:</td>
24                 <td><input type="text" name="name" value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>"/></td>
25             </tr>
26             <tr>
27                 <td>Address:</td>
28                 <td><input type="text" name="address" value="<%= request.getParameter("address") == null ? "" : request.getParameter("address")%>"/></td>
29             </tr>
30             <tr>
31                 <td>Phone:</td>
32                 <td><input type="text" name="phone" value="<%= request.getParameter("phone") == null ? "" : request.getParameter("phone") %>"/></td>
33             </tr>
34             <tr>
35                 <td colspan="2"><input type="submit" value="submit"/></td>
36             </tr>
37         </table>
38     </form>
39 </body>
40 </html>

如果信息增加成功,会重定向到 success.jsp 页面。代码如下:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h4> 操作成功 ^_^</h4>
11     <br>
12     <h4><a href="index.jsp">返回。。。</a></h4>
13 </body>
14 </html>

       然后就是新增加了修改信息的方法,首先会获取你要编辑的那一行的 id,根据 id 显示对应的Customer 的对象的信息,然后再 update 进行修改

所以就是分成了两个动作 edit.do 和 update.do ,在CustomerServlet.java 中补上代码,如下。

 1     private void edit(HttpServletRequest request, HttpServletResponse response) 
 2             throws ServletException, IOException{
 3         
 4         String path = "/error.jsp";
 5         
 6         //1、获取请求参数 id
 7          String idStr = request.getParameter("id");
 8         try {
 9             //2、调用CustomerDao 的 get 方法 获取和 id 对应的 Customer 对象
10             Customer cust= customerDao.get(Integer.parseInt(idStr));
11             if(cust != null){
12                 path = "/updatecustomer.jsp";
13                 //3、将 customer 放入 request 中
14                 request.setAttribute("customers", cust);    
15             }
16         } catch (NumberFormatException e) {
17             e.printStackTrace();
18         }    
19         //4、响应 updatecustomer.jsp 页面:转发。
20         request.getRequestDispatcher(path).forward(request, response);
21     }
22     
23     private void update(HttpServletRequest request, HttpServletResponse response) 
24             throws ServletException, IOException{
25 
26         //1、获取当前 表单的参数 id、name、address、phone、oldName
27         String id = request.getParameter("id");
28         String name = request.getParameter("name");
29         String address = request.getParameter("address");
30         String phone = request.getParameter("phone");
31         String oldName = request.getParameter("oldName");
32         
33         //2、检验name 是否已经被占用。
34         //2.1、比较 name 和 oldName 是否相同,若相同 说明 name 可用。
35         //若不相同,则调用CustomerDao 的 getCountWithName 方法,获取name 在数据库中是否存在
36         if(! oldName.equalsIgnoreCase(name)){
37             long count = customerDao.getCountWithName(name);
38             //2.2、若返回值 > 0,则响应 updatecustomer.jsp 页面:通过转发的方式
39             if(count > 0){
40                 //2.2.1、要求在 updatecustomer.jsp 页面显示一个错误提示:用户名 name 已经被占用,请重新选择。
41                 //在 request 中放入一个属性 message:用户名 name 已经被占用,请重新选择。
42                 //在页面山通过 request.getAttribute("message") 的方式来显示。
43                 request.setAttribute("message", "用户名" + name + "已经被占用,请重新选择。");
44                 //2.2.2、updatecustomer.jsp  的表单可以回显。
45                 
46                 //2.2.3、结束方法:return
47                 request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
48                 return;
49             }
50             
51         }
52         //3、若验证通过,把表单参数封装为一个 Customer 对象 customer
53         Customer customer = new Customer(oldName, address, phone);
54         customer.setId(Integer.parseInt(id));
55         
56         //4、调用 CustomerDao 的update 方法,执行保存操作。
57         customerDao.update(customer);
58         //5.重定向到 query.do 页面
59         response.sendRedirect("query.do");
60     }

调用 customerDao.update 方法时,发现之前写底层的时候 CustomerDao 接口,漏写了这个方法,所以补上:

CustomerDao.java 中补上:

1     /**
2      * 更新
3      * @param customer
4      */
5     public void update(Customer customer);

CustomerDaoJdbcImpl.java 中补上:

1     @Override
2     public void update(Customer customer) {
3         String sql = "update customer set name=?,address=?,phone=? where id=?";
4         update(sql, customer.getName(), customer.getAddress(), customer.getPhone(), customer.getId());
5     }

       我的 修改信息的页面 updatecustomer.jsp  是这样写的,考虑到了这几点:1、不区分大小写,(因为MySQL 里是不区分大小写的原因)。2、因为我的name 是唯一的,所以要修改时,要先判断新的 name 和 旧的name 是否一致,一致的话,就说明这个name 可以用。不一致的话,就得去数据库里查询一下有没有这个记录,有的话,得给出错误提示。3、表单的回显,得考虑如果新的 name 和旧的name 一致,回显的数据就是 新的。如果不一致且新的name 在数据库中有记录,回显的得是旧的数据。

代码如下:

 1 <%@page import="com.hnust.mvcapp.dao.CustomerDao"%>
 2 <%@page import="com.hnuct.mvcapp.entity.Customer"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <%
13         Object mes = request.getAttribute("message");
14         if(mes != null){
15     %>
16             <br>
17             <font color="red"><%= mes %></font>
18     <% 
19         }
20         
21         String id = null;
22         String name = null;
23         String oldName = null;
24         String address = null;
25         String phone = null;
26         Customer customer = (Customer) request.getAttribute("customers");
27         if(customer != null){
28             id = customer.getId() + "";
29             name = customer.getName();
30             oldName = customer.getName();
31             address = customer.getAddress();
32             phone = customer.getPhone();
33         }else{
34             id = request.getParameter("id");
35             name = request.getParameter("oldName");
36             oldName = request.getParameter("oldName");
37             address = request.getParameter("address");
38             phone = request.getParameter("phone");        
39         }
40     %>
41     
42     <form action="update.do" method="post">
43         <input type="hidden" name="id" value="<%= id %>"/>
44         <input type="hidden" name="oldName" value="<%= oldName %>"/>
45         <table>
46             <tr>
47                 <td>Name:</td>
48                 <td><input type="text" name="name" value="<%= name %>"/></td>
49             </tr>
50             <tr>
51                 <td>Address:</td>
52                 <td><input type="text" name="address" value="<%= address %>"/></td>
53             </tr>
54             <tr>
55                 <td>Phone:</td>
56                 <td><input type="text" name="phone" value="<%= phone %>"/></td>
57             </tr>
58             <tr>
59                 <td colspan="2"><input type="submit" value="submit"/></td>
60             </tr>
61         </table>
62     </form>
63 </body>
64 </html>

至此,实现了基本的增删改查的操作,其实里面很多的细节还值得自己今后完善。

有些细节的考虑还值得反复推敲一下,例如:隐藏域的问题,回显的问题,对 name 字段的验证,还有我的添加信息的界面和修改信息的界面其实可以写成一个的。

原文地址:https://www.cnblogs.com/daoxiaobai/p/6270791.html