修改分类步骤分析: 1.在list.jsp页面上点击修改(编辑) /store/adminCategory?method=getById&cid=??? 2.在getById方法中 获取cid 调用service 通过id获取一个分类, 将category放入request域中 请求转发 edit.jsp 3.eidt页面是一个表单 添加action属性 /store/adminCategory?method=update 修改内容 点击提交 添加隐藏域 cid 4.在update方法中 获取cid和cname 封装成category 调用service的update方法 重定向/store/adminCategory?method=findAll 5.在service的update方法中 调用dao更新 清空缓存 6.dao中更新数据
/store/WebContent/admin/category/list.jsp
/store/src/com/louis/web/servlet/AdminCategoryServlet.java
/** * 通过id获取分类信息 * @param request * @param response * @return * @throws Exception */ public String getById(HttpServletRequest request, HttpServletResponse response) throws Exception { //1.接受cid String cid = request.getParameter("cid"); //2 调用service完成 查询操作 返回值:category CategoryService cs=(CategoryService) BeanFactory.getBean("CategoryService"); Category c=cs.getById(cid); //3.将category放入request域中, 请求转发 /admin/category/edit.jsp request.setAttribute("bean", c); return "/admin/category/edit.jsp"; }
/store/src/com/louis/service/impl/CategoryServiceImpl.java
/* * 通过cid获取一个分类对象 */ @Override public Category getById(String cid) throws Exception { CategoryDao cd = (CategoryDao) BeanFactory.getBean("CategoryDao"); return cd.getById(cid); }
/store/src/com/louis/dao/impl/CategoryDaoImpl.java
/** * 通过id获取一个分类 */ @Override public Category getById(String cid) throws Exception { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from category where cid = ? limit 1"; return qr.query(sql, new BeanHandler<>(Category.class), cid); }
/store/WebContent/admin/category/edit.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <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"> </HEAD> <body> <form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminCategory?method=update" method="post"> <!-- 隐藏域 存放cid --> <input type="hidden" name="cid" value="${bean.cid }"> <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" colspan="3"> <input type="text" name="cname" id="userAction_save_do_logonName" value="${bean.cname }" 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> <FONT face="宋体"> </FONT> <button type="reset" value="重置" class="button_cancel">重置</button> <FONT face="宋体"> </FONT> <INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/> <span id="Label1"></span> </td> </tr> </table> </form> </body> </HTML>
/store/WebContent/admin/category/edit.jsp
/store/src/com/louis/web/servlet/AdminCategoryServlet.java
/** * 更新分类信息方法 * @param request * @param response * @return * @throws Exception */ public String update(HttpServletRequest request, HttpServletResponse response) throws Exception { //1.获取cid cname //2.封装参数 Category c=new Category(); c.setCid(request.getParameter("cid")); c.setCname(request.getParameter("cname")); //3.调用service 完成更新操作 CategoryService cs=(CategoryService) BeanFactory.getBean("CategoryService"); cs.update(c); //4.重定向 查询所有 response.sendRedirect(request.getContextPath()+"/adminCategory?method=findAll"); return null; }
/store/src/com/louis/service/impl/CategoryServiceImpl.java
/** * 更新分类 */ @Override public void update(Category c) throws Exception { //1.调用dao更新 CategoryDao cd = (CategoryDao) BeanFactory.getBean("CategoryDao"); cd.update(c); //2.清空缓存 CacheManager cm = CacheManager.create(CategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml")); Cache cache = cm.getCache("categoryCache"); cache.remove("clist"); }
/store/src/com/louis/dao/impl/CategoryDaoImpl.java
/** * 更新 */ @Override public void update(Category c) throws Exception { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="update category set cname = ? where cid = ?"; qr.update(sql, c.getCname(),c.getCid()); }
问题
1、为什么要使用隐藏域
答:因为是cid