zoukankan      html  css  js  c++  java
  • 记账本开发记录——第十九天(2020.2.6)

    今天主要学习了如何使用各种工具类和数据库连接池来做一个增删改查。算是当作一个复习,

    首先是显示所有,这点不再说了,一直在做。

    然后是增加,这里使用了各种工具类,并且为了一些数据的回显写了个servlet

    界面:

      1 <%@ page language="java" pageEncoding="UTF-8"%>
      2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
      3 <HTML>
      4     <HEAD>
      5         <meta http-equiv="Content-Language" content="zh-cn">
      6         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      7         <LINK href="${pageContext.request.contextPath}/css/Style1.css" type="text/css" rel="stylesheet">
      8     </HEAD>
      9     
     10     <body>
     11         <!--  -->
     12         <form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminAddProduct" method="post" >
     13             &nbsp;
     14             <table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
     15                 <tr>
     16                     <td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
     17                         height="26">
     18                         <strong><STRONG>添加商品</STRONG>
     19                         </strong>
     20                     </td>
     21                 </tr>
     22 
     23                 <tr>
     24                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     25                         商品名称:
     26                     </td>
     27                     <td class="ta_01" bgColor="#ffffff">
     28                         <input type="text" name="pname" value="" id="userAction_save_do_logonName" class="bg"/>
     29                     </td>
     30                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     31                         是否热门:
     32                     </td>
     33                     <td class="ta_01" bgColor="#ffffff">
     34                         <select name="is_hot">
     35                             <option value="1"></option>
     36                             <option value="0"></option>
     37                         </select>
     38                     </td>
     39                 </tr>
     40                 <tr>
     41                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     42                         市场价格:
     43                     </td>
     44                     <td class="ta_01" bgColor="#ffffff">
     45                         <input type="text" name="market_price" value="" id="userAction_save_do_logonName" class="bg"/>
     46                     </td>
     47                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     48                         商城价格:
     49                     </td>
     50                     <td class="ta_01" bgColor="#ffffff">
     51                         <input type="text" name="shop_price" value="" id="userAction_save_do_logonName" class="bg"/>
     52                     </td>
     53                 </tr>
     54                 <tr>
     55                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     56                         商品图片:
     57                     </td>
     58                     <td class="ta_01" bgColor="#ffffff" colspan="3">
     59                         <input type="file" name="upload" />
     60                     </td>
     61                 </tr>
     62                 <tr>
     63                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     64                         所属分类:
     65                     </td>
     66                     <td class="ta_01" bgColor="#ffffff" colspan="3">
     67                         <select name="cid">
     68                         <c:forEach items="${category }" var="category">
     69                         <option value="${category.cid}">${category.cname}</option>
     70                         </c:forEach>
     71                         </select>
     72                     </td>
     73                 </tr>
     74                 <tr>
     75                     <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
     76                         商品描述:
     77                     </td>
     78                     <td class="ta_01" bgColor="#ffffff" colspan="3">
     79                         <textarea name="pdesc" rows="5" cols="30"></textarea>
     80                     </td>
     81                 </tr>
     82                 <tr>
     83                     <td class="ta_01" style="WIDTH: 100%" align="center"
     84                         bgColor="#f5fafe" colSpan="4">
     85                         <button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok">
     86                             确定
     87                         </button>
     88 
     89                         <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
     90                         <button type="reset" value="重置" class="button_cancel">&#37325;&#32622;</button>
     91 
     92                         <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
     93                         <INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
     94                         <span id="Label1"></span>
     95                     </td>
     96                 </tr>
     97             </table>
     98         </form>
     99     </body>
    100 </HTML>
    添加界面

    关于数据回显的servlet:

     1 package web;
     2 
     3 import java.io.IOException;
     4 import java.sql.SQLException;
     5 import java.util.List;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 import domain.Category;
    14 import service.AdminProductService;
    15 
    16 /**
    17  * Servlet implementation class AdminAddProductUIServlet
    18  */
    19 @WebServlet("/adminAddProductUI")
    20 public class AdminAddProductUIServlet extends HttpServlet {
    21     private static final long serialVersionUID = 1L;
    22        
    23     /**
    24      * @see HttpServlet#HttpServlet()
    25      */
    26     public AdminAddProductUIServlet() {
    27         super();
    28         // TODO Auto-generated constructor stub
    29     }
    30 
    31     /**
    32      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    33      */
    34     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    35         
    36         //获得所有的商品类别数据
    37         AdminProductService service = new AdminProductService();
    38         List<Category> categoryList = null;
    39         try {
    40             categoryList = service.findAllCategory();
    41         } catch (SQLException e) {
    42             // TODO Auto-generated catch block
    43             e.printStackTrace();
    44         }
    45         request.setAttribute("category", categoryList);
    46         request.getRequestDispatcher("/admin/product/add.jsp").forward(request, response);
    47     }
    48 
    49     /**
    50      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    51      */
    52     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    53         // TODO Auto-generated method stub
    54         doGet(request, response);
    55     }
    56 
    57 }
    添加的UI显示servlet

    添加数据的servlet:

    package web;
    
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    import java.util.UUID;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.beanutils.BeanUtils;
    
    import com.sun.org.apache.bcel.internal.generic.NEW;
    
    import domain.Product;
    import service.AdminProductService;
    
    /**
     * Servlet implementation class AdminAddProductServlet
     */
    @WebServlet("/adminAddProduct")
    public class AdminAddProductServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public AdminAddProductServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            //1.获取数据
            Map<String, String[]> properties = request.getParameterMap();
            //2.封装数据
            Product product =new Product();
            try {
                BeanUtils.populate(product, properties);
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            //此位置Product已经封装完毕----将表单的数据封装完毕
            //手动设置表单中没有数据
            //private String pid
            product.setPid(UUID.randomUUID().toString());
            //pimage
            product.setPimage("products/1/c_0013.jpg");
            //pdate  //上架日期
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String pdate = format.format(new Date());
            product.setPdate(pdate);
            //pflag //是否下架 0代表未下架
            product.setPflag(0);
            //3.传递数据给service层
            AdminProductService service = new AdminProductService();
            service.addProduct(product);
            
            //跳转到列表页面
            response.sendRedirect(request.getContextPath()+"/adminProductList");
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    service:

    package service;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import dao.AdminProductDao;
    import domain.Category;
    import domain.Product;
    
    public class AdminProductService {
        
        //查询所有的商品
        public List<Product> findAllProduct() throws SQLException {
            //因为没有复杂业务 直接传递请求到dao层
            AdminProductDao dao = new AdminProductDao();
            return dao.findAllProduct();
        }
        
        //获得所有的类别
        public List<Category> findAllCategory() throws SQLException {
            AdminProductDao dao = new AdminProductDao();
            return dao.findAllCategory();
        }
        
        //添加商品
        public void addProduct(Product product) {
            
            AdminProductDao dao = new AdminProductDao();
            try {
                dao.addProduct(product);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    
    }

    dao层代码就不再贴了,都是常规了

    下面是修改的数据回显,主要是学习了使用js和jq来做到下拉框的选中

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <HTML>
        <HEAD>
            <meta http-equiv="Content-Language" content="zh-cn">
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <LINK href="${pageContext.request.contextPath}/css/Style1.css" type="text/css" rel="stylesheet">
            
            <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
            
            <script type="text/javascript">
            
                $(function(){
                    //获得当前回显的product的cid
                    $("#cid option[value='${product.cid }']").prop("selected",true);
                    //是否热门
                    $("#is_hot option[value='${product.is_hot }']").prop("selected",true);
                });
            
            
                //页面加载完毕后 确定那个option被选中
                /* window.onload = function(){
                    //获得当前回显的product的cid
                    var cid = "${product.cid }";
                    //获得所有的<select name="cid">下的option
                    var options = document.getElementById("cid").getElementsByTagName("option");
                    //比较每一个option的value与cid
                    for(var i=0;i<options.length;i++){
                        if(cid==options[i].value){
                            options[i].selected = true;
                        }
                    }
                } */
                
                
                
            </script>
            
            
        </HEAD>
        
        <body>
            <!--  -->
            <form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminUpdateProduct" method="post">
                
                <input type="hidden" name="pid" value="${product.pid }">
                
                <table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
                    <tr>
                        <td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
                            height="26">
                            <strong><STRONG>编辑商品</STRONG>
                            </strong>
                        </td>
                    </tr>
    
                    <tr>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            商品名称:
                        </td>
                        <td class="ta_01" bgColor="#ffffff">
                            <input type="text" name="pname" value="${product.pname }" id="userAction_save_do_logonName" class="bg"/>
                        </td>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            是否热门:
                        </td>
                        <td class="ta_01" bgColor="#ffffff">
                            
                            <select id="is_hot" name="is_hot">
                                <option value="1">是</option>
                                <option value="0">否</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            市场价格:
                        </td>
                        <td class="ta_01" bgColor="#ffffff">
                            <input type="text" name="market_price" value="${product.market_price }" id="userAction_save_do_logonName" class="bg"/>
                        </td>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            商城价格:
                        </td>
                        <td class="ta_01" bgColor="#ffffff">
                            <input type="text" name="shop_price" value="${product.shop_price }" id="userAction_save_do_logonName" class="bg"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            商品图片:
                        </td>
                        <td class="ta_01" bgColor="#ffffff" colspan="3">
                            <input type="file" name="upload" />
                        </td>
                    </tr>
                    <tr>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            所属分类:
                        </td>
                        <td class="ta_01" bgColor="#ffffff" colspan="3">
                            <select id="cid" name="cid">
                                <c:forEach items="${categoryList }" var="category">
                                    <option value="${category.cid }">${category.cname }</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                            商品描述:
                        </td>
                        <td class="ta_01" bgColor="#ffffff" colspan="3">
                            <textarea name="pdesc" rows="5" cols="30">${product.pdesc }</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td class="ta_01" style="WIDTH: 100%" align="center"
                            bgColor="#f5fafe" colSpan="4">
                            <button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok">
                                确定
                            </button>
    
                            <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
                            <button type="reset" value="重置" class="button_cancel">&#37325;&#32622;</button>
    
                            <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
                            <INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
                            <span id="Label1"></span>
                        </td>
                    </tr>
                </table>
            </form>
        </body>
    </HTML>

    可以看到,js和jq在这里起到了真正的用处。

    明日任务:完成记账本web端的增删改查 (预计使用模态框 bootstrap等等来实现一个较为醒目的效果)

  • 相关阅读:
    在胜利中窥探危机、在失败中寻觅良机
    自我剖析--为了更好的自己
    Python os模块之文件操作
    Python:XXX missing X required positional argument: 'self'
    Python scipy.sparse矩阵使用方法
    计算机视觉算法框架理解
    Python--Argparse学习感悟
    ROC曲线、AUC、Precision、Recall、F-measure理解及Python实现
    Windows版的各种Python库安装包下载地址与安装过程
    NLP常见任务
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/12270133.html
Copyright © 2011-2022 走看看