码迷,mamicode.com
首页 > 编程语言 > 详细

[原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

时间:2016-05-27 23:22:30      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 新增操作:

  思路:在index.jsp 页面下,点击Add New Customer 超链接,转向newcustomer.jsp 。在添加的操作有两个需求:

    1.保证name的唯一性,也就是说,在新增的用户中,先进行查询name在数据库中是否存在。

       若存在,则通过转发发送到newcustomer.jsp 一条提示信息;

       若不存,则将请求的信息name,address,phone封装为一个Customer的对象,在通过调用customerDAO的save(),将信息添加到数据库,并且通过重定向的方式跳转到 success.jsp,加上提示 和 超链接 返回 querey.do 

    2.在index.jsp 页面要有回显的功能。也就是说不过成功添加还是不成功添加,都要将输入的信息显示到输入框中,达到回显的功能。

  

  关键代码:

  1.index.jsp 中的 Add New Customer 超链接

 

 1 <body>
 2 
 3     <form action="query.do" method="post">
 4         <table>
 5             <tr>
 6                  <td>CustomerName:</td>
 7                  <td><input type="text" name="name"/></td>
 8             </tr>
 9             <tr>
10                  <td>Address:</td> 
11                  <td><input type="text" name="address"/></td>
12             </tr>
13             <tr>
14                  <td>Phone:</td>
15                  <td><input type="text" name="phone"/></td>
16             </tr>
17             <tr>
18                  <td><input type="submit" value="Query"/></td>
19                  <td><a href="newcustomer.jsp">Add New Customer</a></td>
20             </tr>
21         </table>
22     </form>
23     <br><br>

 

  

  2.newcustomer.jsp 页面

   说明:① 通过   value="<%= request.getParameter("name") == null ? "" : request.getParameter("name") %>" 方式 达到回显的效果

      ② 当添加的name 在数据库从在,servlet 会返回一个message 信息。通过 <h2><font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font></h2> 方式

        达到提示的效果

 

 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 <body>
11             <%--可以用来回显  <%= request.getParameter("name") %> 若为空 则赋为 空串儿 ; 反之 ,赋原值 --%>
12 
13     <h2><font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font></h2>
14     
15     <form action="addCustomer.do" method="post">
16         <table>
17             <tr>
18                  <td>CustomerName:</td>
19                  <td><input type="text" name="name" 
20                   value="<%= request.getParameter("name") == null ? "" : request.getParameter("name")  %>"/></td>
21             </tr>
22             <tr>
23                  <td>Address:</td> 
24                  <td><input type="text" name="address"
25                   value="<%= request.getParameter("address") == null ? "" : request.getParameter("address")%>"/></td>
26             </tr>
27             <tr>
28                  <td>Phone:</td>
29                  <td><input type="text" name="phone" 
30                  value="<%= request.getParameter("phone") == null ? "" : request.getParameter("phone") %>"/></td>
31             </tr>
32             <tr>
33                  <td colspan="2"><input type="submit" value="submit"/></td>
34             </tr>
35         </table>
36     </form>
37 
38 </body>
39 </html>

 

  

  3.success,jsp :提示信息和返回index.jsp 的超链接

 

1 <body>
2     
3     
4     <h1>操作成功</h1>
5     <a href="index.jsp">return ....</a>
6     
7     
8 </body>

 

 

  3.CustomerServlet2 中的addCustomer()方法

  

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

 

 

  4.CustomerDAO 中的 save 声明 ,getCountWithName()

 

1 //保存操作
2     public void save(Customer coustomer);
3 
4 
5 //查看与参数相同的名字有多少个记录数
6     public long getCountWithName(String name);

 

 

  5.CustomerDAOJdbcImpl 对CustomerDAO 的 save()具体实现 ,  getCountWithName

 

 1 @Override
 2     public void save(Customer customer) {
 3         String sql = "INSERT INTO customers(name, address, phone) VALUES(?,?,? )";
 4         update(sql,customer.getName(),customer.getAddress(),customer.getPhone());
 5     }
 6 
 7 @Override
 8     public long getCountWithName(String name) {
 9         String sql = "SELECT count(id) FROM customers WHERE name = ?";
10         return getForValue(sql, name);
11     }

 

 

 

  6.DAO 中的update() ,getForValue

  

    /**
     * @param sql
     *            : sql语句
     * @param ags
     *            : sql语句的占位符
     * @description:该方法封装了 INSERT ,DELETE,UPDATE 操作
     */
    public void update(String sql, Object... ags){
        
        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();    
            queryRunner.update(connection, sql, ags);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JdbcUtils.releaseConnection(connection);
        }
    }


/**
     * @param sql
     * @param args
     * @description:返回某一个字段的值,例如:返回某一条记录的customerName
     */
    public <E> E getForValue(String sql, Object... args) {
        
        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();
            return (E) queryRunner.query(connection, sql, new ScalarHandler(), args);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            JdbcUtils.releaseConnection(connection);
        }
        return null;
    }

 

 

 

  7.总结

  1)从代码层次理解MVC

  2)对于添加:一方面 要验证;一方面要更新数据库

  

 

[原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

标签:

原文地址:http://www.cnblogs.com/jasonHome/p/5536221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!