网上图书商城项目学习笔记-027添加二级分类

一、流程分析

网上图书商城项目学习笔记-027添加二级分类_第1张图片网上图书商城项目学习笔记-027添加二级分类_第2张图片

 

 

二、代码

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)add2.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(!$("#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='addChild'/>
30         分类名称:<input type="text" name="cname" id="cname"/><br/>
31         一级分类:<select name="pid" id="pid">
32             <option value="">===选择1级分类===</option>
33         <c:forEach items="${parents }" var="parent">
34             <option value="${parent.cid }" <c:if test="${parent.cid eq pid }">selected="selected"</c:if>>${parent.cname }</option>
35         </c:forEach>
36         </select><br/>
37         分类描述:<textarea rows="5" cols="50" name="desc" id="desc"></textarea><br/>
38         <input type="submit" value="添加二级分类"/>
39         <input type="button" value="返回" onclick="history.go(-1)"/>
40     </form>
41   </body>
42 </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 addChildPre(HttpServletRequest req, HttpServletResponse resp)
10             throws ServletException, IOException {
11         String pid = req.getParameter("pid");
12         List<Category> parents = service.findParents();
13         req.setAttribute("pid", pid);
14         req.setAttribute("parents", parents);
15         return "/adminjsps/admin/category/add2.jsp";
16     }
17     
18     /**
19      * 添加二级
20      * @param req
21      * @param resp
22      * @return
23      * @throws ServletException
24      * @throws IOException
25      */
26     public String addChild(HttpServletRequest req, HttpServletResponse resp)
27             throws ServletException, IOException {
28         Category child = CommonUtils.toBean(req.getParameterMap(), Category.class);
29         child.setCid(CommonUtils.uuid());
30         
31         Category parent = new Category();
32         parent.setCid(req.getParameter("pid"));
33         child.setParent(parent);
34         
35         service.add(child);
36         return findAll(req, resp);
37     }

 

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     }
12 
13     /**
14      * 添加分类
15      * @param category
16      */
17     public void add(Category category) {
18         try {
19             categoryDao.add(category);
20         } catch (SQLException e) {
21             throw new RuntimeException(e);
22         }
23     }

 

4.dao层

(1)AdminCategroyDao.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     }
13 
14     /**
15      * 添加分类
16      * @param category
17      * @throws SQLException
18      */
19     public void add(Category category) throws SQLException {
20         String pid = null;
21         if(category.getParent() != null) {
22             pid = category.getParent().getCid();
23         }
24         String sql = "insert into t_category(cid, cname, pid, `desc`) values (?,?,?,?)";
25         Object [] params = {category.getCid(), category.getCname(), pid, category.getDesc()};
26         qr.update(sql, params);
27     }

 

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