• 网上图书商城项目学习笔记-028编辑一级分类


    一、流程分析

    二、代码

    1.view层

    (1)list.jsp

     1 <c:forEach items="${parents }" var="parent">
     2         <tr class="trOneLevel">
     3             <td width="200px;">${parent.cname }</td>
     4             <td>${parent.desc }</td>
     5             <td width="200px;">
     6               <a href="<c:url value='/admin/AdminCategoryServlet?method=addChildPre&pid=${parent.cid }'/>">添加二级分类</a>
     7               <a href="<c:url value='/admin/AdminCategoryServlet?method=editParentPre&cid=${parent.cid }'/>">修改</a>
     8               <a onclick="return confirm('您是否真要删除该一级分类?')" href="<c:url value='/admin/AdminCategoryServlet?method=deleteParent&cid=${parent.cid }'/>">删除</a>
     9             </td>
    10         </tr>
    11     <c:forEach items="${parent.children }" var="child">
    12         <tr class="trTwoLevel">
    13             <td>${child.cname }</td>
    14             <td>J${child.desc }</td>
    15             <td width="200px;" align="right">
    16               <a href="<c:url value='/admin/AdminCategoryServlet?method=editChildPre&cid=${child.cid }'/>">修改</a>
    17               <a onclick="return confirm('您是否真要删除该二级分类?')" href="<c:url value='/admin/AdminCategoryServlet?method=deleteChild&cid=${child.cid }'/>">删除</a>
    18             </td>
    19         </tr>
    20     </c:forEach>
    21 </c:forEach>

    (2)edit.jsp

     

     1     <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script>
     2     <script type="text/javascript">
     3         function checkForm() {
     4             if(!$("#cname").val()) {
     5                 alert("分类名不能为空!");
     6                 return false;
     7             }
     8             if(!$("#desc").val()) {
     9                 alert("分类描述不能为空!");
    10                 return false;
    11             }
    12             return true;
    13         }
    14     </script>
    15 <style type="text/css">
    16     body {background: rgb(254,238,189);}
    17 </style>
    18   </head>
    19   
    20   <body>
    21     <h3>修改1级分类</h3>
    22     <h1></h1>
    23     <p style="font-weight: 900; color: red">${msg }</p>
    24     <form action="<c:url value='/admin/AdminCategoryServlet'/>" method="post" onsubmit="return checkForm()">
    25         <input type="hidden" name="cid" value="${parent.cid }"/>
    26         <input type="hidden" name="method" value="editParent"/>
    27         分类名称:<input type="text" name="cname" id="cname" value="${parent.cname }"/><br/>
    28         分类描述:<textarea rows="5" cols="50" id="desc" name="desc">${parent.desc }</textarea><br/>
    29         <input type="submit" value="修改分类"/>
    30         <input type="button" value="返回" onclick="history.go(-1)"/>
    31     </form>
    32   </body>
    33 </html>

    2.servlet层

    (1)AdminCategoryServlet.java

     

     1     /**
     2      * 准备编辑一级分类
     3      * @param req
     4      * @param resp
     5      * @return
     6      * @throws ServletException
     7      * @throws IOException
     8      */
     9     public String editParentPre(HttpServletRequest req, HttpServletResponse resp)
    10             throws ServletException, IOException {
    11         req.setAttribute("parent", service.load(req.getParameter("cid")));
    12         return "/adminjsps/admin/category/edit.jsp";
    13     }
    14     
    15     /**
    16      * 编辑一级分类
    17      * @param req
    18      * @param resp
    19      * @return
    20      * @throws ServletException
    21      * @throws IOException
    22      */
    23     public String editParent(HttpServletRequest req, HttpServletResponse resp)
    24             throws ServletException, IOException {
    25         Category parent = CommonUtils.toBean(req.getParameterMap(), Category.class);
    26         service.edit(parent);
    27         return findAll(req, resp);
    28     }

    3.service层

    (1)AdminCategoryService.java 

     1     /**
     2      * 加载分类
     3      * @param cid
     4      * @return
     5      */
     6     public Category load(String cid) {
     7         try {
     8             return categoryDao.load(cid);
     9         } catch (SQLException e) {
    10             throw new RuntimeException(e);
    11         }
    12     }
    13     
    14     /**
    15      * 修改分类
    16      * @param category
    17      */
    18     public void edit(Category category) {
    19         try {
    20             categoryDao.edit(category);
    21         } catch (SQLException e) {
    22             throw new RuntimeException(e);
    23         }
    24     }

    4.dao层

    (1)AdminCategoryDao.java

     1 /**
     2      * 加载分类
     3      * @param cid
     4      * @return
     5      * @throws SQLException
     6      */
     7     public Category load(String cid) throws SQLException {
     8         String sql = "select * from t_category where cid=?";
     9         Map<String,Object> map = qr.query(sql, new MapHandler(), cid);
    10         return toCategory(map);
    11     }
    12     
    13     /**
    14      * 修改分类
    15      * @param category
    16      * @throws SQLException
    17      */
    18     public void edit(Category category) throws SQLException {
    19         String sql = "update t_category set cname=?, pid=?, `desc`=? where cid=?";
    20         String pid = null;
    21         if(category.getParent() != null) {
    22             pid = category.getParent().getCid();
    23         }
    24         Object [] params = {category.getCname(), pid, category.getDesc(), category.getCid()};
    25         qr.update(sql, params);
    26     }
  • 相关阅读:
    png图片在ie不透明的解决方案
    ASP如何查询ACCESS数据库中上一周的所有记录
    使用FSO修改文件夹的名称
    两组字符串数据比较合并相同数据
    1272 并查集
    1232 并查集
    What's New in ASP.NET 4.5 and Visual Web Developer 11 Developer Preview
    knockout.js
    .net training
    Unknown server tag 'asp:ListView'. sharepoint
  • 原文地址:https://www.cnblogs.com/shamgod/p/5181524.html
Copyright © 2020-2023  润新知