修改分类步骤分析:
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 }">
&nbsp;
<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">
确&#23450;
</button> <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
<button type="reset" value="重置" class="button_cancel">重置</button> <FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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

最新文章

  1. 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
  2. 廖雪峰python教程的第一个疑问
  3. Linux下EclipseCDT工程和TFS的持续集成CI实践
  4. [BZOJ1116][Poi2008]LCO(并查集)
  5. 由浅到深讲解C#-LINQ
  6. 注意!你的Thread.Abort方法真的让线程停止了吗?
  7. eclipse创建java类时自动添加注释
  8. 用Python编写九九乘法表考虑print自动换行问题
  9. 5.3:从bean的实例中获取对象
  10. 理解 bashrc 和 profile(转)
  11. tcpdump抓包工具的使用
  12. 记录python接口自动化测试--主函数(第六目)
  13. 自己动手写CPU(基于FPGA与Verilog)
  14. MongoDB的安装和使用
  15. Python学习第三篇——逻辑判定
  16. 剑指offer(20)包含min函数的栈
  17. Nginx + Uswgi + Django的部署
  18. 2017-2018-2 20155303『网络对抗技术』Exp4:恶意代码分析
  19. RabbitMQ、Redis、Memcache
  20. tensorflow 指定使用gpu处理,tensorflow占用多个GPU但只有一个在跑

热门文章

  1. mysql explain 的type解释
  2. vim中末行去掉^M
  3. 如何干掉那些.ipch 与 .sdf文件
  4. HDOJ How many ways?? 2157【矩阵高速幂】
  5. Handlebars的基本用法 Handlebars.js使用介绍 http://handlebarsjs.com/ Handlebars.js 模板引擎 javascript/jquery模板引擎——Handlebars初体验 handlebars.js 入门(1) 作为一名前端的你,必须掌握的模板引擎:Handlebars 前端数据模板handlebars与jquery整
  6. Java 兔子问题(斐波那契数列)扩展篇
  7. ViewPagerIndicator
  8. 用python编写的定向arp欺骗工具
  9. 将MySQL服务绑定到固定的IP地址上
  10. liberOJ #6173. Samjia 和矩阵 hash+后缀数组