网上图书商城项目学习笔记-029编辑二级分类

一、流程分析

网上图书商城项目学习笔记-029编辑二级分类_第1张图片

二、代码

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)edit2.js

 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(!$("#pid").val()) {
 9                 alert("一级分类不能为空!");
10                 return false;
11             }
12             if(!$("#desc").val()) {
13                 alert("分类描述不能为空!");
14                 return false;
15             }
16             return true;
17         }
18     </script>
19 <style type="text/css">
20     body {background: rgb(254,238,189);}
21 </style>
22   </head>
23   
24   <body>
25     <h3>修改2级分类</h3>
26     <h1></h1>
27     <p style="font-weight: 900; color: red">${msg }</p>
28     <form action="<c:url value='/admin/AdminCategoryServlet'/>" method="post" onsubmit="return checkForm()">
29         <input type="hidden" name="method" value="editChild"/>
30         <input type="hidden" name="cid" value="${child.cid }"/>
31         分类名称:<input type="text" name="cname" value="${child.cname }" id="cname"/><br/>
32         一级分类:<select name="pid" id="pid">
33             <option value="">===选择1级分类===</option>
34         <c:forEach items="${parents }" var="parent">
35             <option value="${parent.cid }" <c:if test="${child.parent.cid eq parent.cid }">selected='selected'</c:if>>${parent.cname }</option>
36         </c:forEach>
37         </select><br/>
38         分类描述:<textarea rows="5" cols="50" name="desc" id="desc">${child.desc }</textarea><br/>
39         <input type="submit" value="修改二级分类"/>
40         <input type="button" value="返回" onclick="history.go(-1)"/>
41     </form>
42   </body>
43 </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 editChildPre(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         Category child = service.load(req.getParameter("cid"));
12         List<Category> parents = service.findParents();
13         req.setAttribute("child", child);
14         req.setAttribute("parents", parents);
15         return "/adminjsps/admin/category/edit2.jsp";
16     }
17     
18     /**
19      * 编辑二级分类
20      * @param req
21      * @param resp
22      * @return
23      * @throws ServletException
24      * @throws IOException
25      */
26     public String editChild(HttpServletRequest req, HttpServletResponse resp)
27             throws ServletException, IOException {
28         Category child = CommonUtils.toBean(req.getParameterMap(), Category.class);
29         Category parent = new Category();
30         parent.setCid(req.getParameter("pid"));
31         child.setParent(parent);
32         service.edit(child);
33         return findAll(req, resp);
34     }

 

3.service层

(1)AdminCategoryService.java 

 1     /**
 2      * 查找所有一级分类
 3      * @return
 4      */
 5     public List<Category> findParents() {
 6         try {
 7             return categoryDao.findParents();
 8         } catch (SQLException e) {
 9             throw new RuntimeException(e);
10         }
11     }

 

4.dao层

(1)AdminCategoryDao.java

 1     /**
 2      * 查询所有一级分类
 3      * @return
 4      * @throws SQLException
 5      */
 6     public List<Category> findParents() throws SQLException {
 7         // 1. 查询出所有一级分类
 8         String sql = "SELECT * FROM t_category WHERE pid IS NULL ORDER BY orderBy";
 9         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
10         List<Category> parents = toCategoryList(mapList);
11         return parents;
12     }

 

其他方法和编辑一级相同

你可能感兴趣的:(网上图书商城项目学习笔记-029编辑二级分类)