1 查询所有分类
创建AdminCategoryServlet,添加findAllCats方法,查询所有分类
public String findAllCats(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//获取全部分类信息
CategoryService categoryService = new CategoryServiceImp();
List<Category> list = categoryService.findAllCats();
//全部分类信息放入request
req.setAttribute("allCats", list);
//转发到/admin/category/list.jsp
return "/admin/category/list.jsp";
}
编写/admin/category/list.jsp
varStatus:代表循环过程中存储临时状态值
${status.count}:当前输出元素个数
<body>
<br>
<table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0">
<TBODY>
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3">
<strong>分类列表</strong>
</TD>
</tr>
<tr>
<td class="ta_01" align="right">
<button type="button" id="add" name="add" value="添加" class="button_add" onclick="addCategory()">添加</button>
</td>
</tr>
<tr>
<td class="ta_01" align="center" bgColor="#f5fafe">
<table cellspacing="0" cellpadding="1" rules="all"
bordercolor="gray" border="1" id="DataGrid1"
style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
<tr
style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">
<td align="center" width="18%">
序号
</td>
<td align="center" width="17%">
分类名称
</td>
<td width="7%" align="center">
编辑
</td>
<td width="7%" align="center">
删除
</td>
</tr>
<c:forEach items="${allCats}" var="c" varStatus="status">
<tr onmouseover="this.style.backgroundColor = 'white'"
onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="18%">
${
status.count}
</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">
${
c.cname}
</td>
<td align="center" style="HEIGHT: 22px">
<a href="edit.jsp">
<img src="${pageContext.request.contextPath}/img/admin/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px">
<a href="#">
<img src="${pageContext.request.contextPath}/img/admin/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</TBODY>
</table>
</body>
2 添加分类
修改“添加”按钮链接
<td class="ta_01" align="right">
<button type="button" id="add" name="add" value="添加" class="button_add" onclick="addCategory()">添加</button>
</td>
<script type="text/javascript">
function addCategory(){
window.location.href = "${pageContext.request.contextPath}/adminCategoryServlet?method=addCategoryUI";
}
</script>
编写addCategory方法,添加分类
//addCategoryUI
public String addCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
return "/admin/category/add.jsp";
}
//addCategory
public String addCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//获取分类名称
String cname = req.getParameter("cname");
//创建分类ID
String id = UUIDUtils.getId();
Category c = new Category();
c.setCid(id);
c.setCname(cname);
c.setIsvalid(1);
//调用业务层添加分类功能
CategoryService categoryService = new CategoryServiceImp();
categoryService.addCategory(c);
//重定向到查询全部分类信息
resp.sendRedirect( req.getContextPath()+"/adminCategoryServlet?method=findAllCats");
return null;
}
编写业务层
需要及时更新redis中的缓存信息。
虽然使用redis可以提升项目性能,但是带来开发量。开发中,如果对redis缓存中的数据发生更新操作,及时更新redis缓存信息,否则会造成数据不统一问题。
@Override
public void addCategory(Category c) throws Exception {
//本质是向MYSQL插入一条数据
categoryDao.addCategory(c);
//更新redis缓存
Jedis jedis = JedisPoolUtils.getJedis();
jedis.del("allCats");
jedis.close();
}
编写dao层
向数据库插入数据
@Override
public void addCategory(Category category) {
String sql="insert into category values ( ? ,?,?)";
template.update(sql,category.getCid(),category.getCname(),category.getIsvalid());
}
编写admin/category/add.jsp
编写添加分类页面
<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminCategoryServlet?method=addCategory" method="post">
<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>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
分类名称:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<input type="text" name="cname" value="" id="userAction_save_do_logonName" class="bg"/>
</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>
<span style="font-family: 宋体; "> </span>
<button type="reset" value="重置" class="button_cancel">重置</button>
<span style="font-family: 宋体; "> </span>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"></INPUT>
<span id="Label1"></span>
</td>
</tr>
</table>
</form>
3 编辑分类
修改编辑图标链接
<td align="center" style="HEIGHT: 22px">
<a href="${pageContext.request.contextPath}/adminCategoryServlet?method=editCategoryUI&cid=${c.cid}">
<img src="${pageContext.request.contextPath}/img/admin/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
转到编辑页面
//editCategoryUI
public String editCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String cid = req.getParameter("cid");
req.setAttribute("cid",cid);
return "/admin/category/edit.jsp";
}
编写edit.jsp页面
表单提交到action="${pageContext.request.contextPath}/adminCategoryServlet?method=editCategory&cid=${requestScope.cid}"
<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminCategoryServlet?method=editCategory&cid=${requestScope.cid}" method="post">
<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>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
分类名称:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<input type="text" name="cname" value="${ category.cname }" id="userAction_save_do_logonName" class="bg"/>
</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>
<span style="font-family: 宋体; "> </span>
<button type="reset" value="重置" class="button_cancel">重置</button>
<span style="font-family: 宋体; "> </span>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
<span id="Label1"></span>
</td>
</tr>
</table>
</form>
编写editCategory方法
public String editCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//获取分类名称
String cname = req.getParameter("cname");
//创建分类ID
String id = req.getParameter("cid");
Category c = new Category();
c.setCid(id);
c.setCname(cname);
//调用业务层添加分类功能
CategoryService categoryService = new CategoryServiceImp();
categoryService.editCategory(c);
//重定向到查询全部分类信息
resp.sendRedirect( req.getContextPath()+"/adminCategoryServlet?method=findAllCats");
return null;
}
编写业务层editCategory方法
@Override
public void editCategory(Category c) throws Exception {
categoryDao.editCategory(c);
//更新redis缓存
Jedis jedis = JedisPoolUtils.getJedis();
jedis.del("allCats");
jedis.close();
}
编写dao层
更新category表的cname
@Override
public void editCategory(Category c) throws SQLException {
String sql = "UPDATE category SET cname =? WHERE cid =?";
template.update(sql, c.getCname(), c.getCid());
}
4 删除分类
删除分类的时候,由于分类被很多商品所参照,无法删除
- 先删除所有相关联的商品信息,再删除分类。
- 设置所有商品的cid列的值为null/其他分类ID
- 设计分类表的时候多增加一个列(有效/无效)
利用第3种方法来删除分类
添加删除分类方法
//删除分类
public String delCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//获取cid
String cid = req.getParameter("cid");
//业务层
CategoryService categoryService = new CategoryServiceImp();
categoryService.delCategory(cid);
重定向
resp.sendRedirect( req.getContextPath()+"/adminCategoryServlet?method=findAllCats");
return null;
}
业务层,更新数据库记忆redis缓存
@Override
public void delCategory(String cid) throws Exception {
categoryDao.delCategory(cid);
//更新redis缓存
Jedis jedis = JedisPoolUtils.getJedis();
jedis.del("allCats");
jedis.close();
}
dao层,更新isvalid = 0
@Override
public void delCategory(String cid) throws SQLException {
String sql = "UPDATE category SET isvalid = 0 WHERE cid =?";
template.update(sql, cid);
}
修改dao层,查询所有分类语句
public List<Category> findAllCats() {
String sql = "select * from category where isvalid=1";
return template.query(sql, new BeanPropertyRowMapper<>(Category.class));
}