目录
1-1-6 点击超链接 发送一个servlet上(FindAllServlet)
4-2-2 点击删除选中,需要将勾选上的商品的id提交到后台
5-1-1 提交的路径 findProductByCondition
6-3-6 以后开发中一般会将这5个参数封装一个javabean(PageBean)
6-4-2 在首页上有一个超链接,点击超链接将第一页查询出来
6-4-4 service.showProductsByPage
-
一、案例1-展示所有商品
需求:
点击首页上的超链接,在页面上展示所有的商品信息
-
1-1 步骤分析
-
1-1-1 数据库和表
create database day14;
use day14;
create table `product` (
`pid` varchar (96),
`pname` varchar (150),
`market_price` double ,
`shop_price` double ,
`pimage` varchar (600),
`pdate` date ,
`pdesc` varchar (765)
);
INSERT INTO `product` VALUES('1','小米 4c 标准版','1399','1299','products/1/c_0001.jpg','2015-11-02','小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待');
INSERT INTO `product` VALUES('10','华为 Ascend Mate7','2699','2599','products/1/c_0010.jpg','2015-11-02','华为 Ascend Mate7 月光银 移动4G手机 双卡双待双通6英寸高清大屏,纤薄机身,智能超八核,按压式指纹识别!!选择下方“移动老用户4G飞享合约”,无需换号,还有话费每月返还!');
INSERT INTO `product` VALUES('11','vivo X5Pro','2399','2298','products/1/c_0014.jpg','2015-11-02','移动联通双4G手机 3G运存版 极光白【购机送蓝牙耳机+蓝牙自拍杆】新升级3G运行内存·双2.5D弧面玻璃·眼球识别技术');
INSERT INTO `product` VALUES('12','努比亚(nubia)My 布拉格','1899','1799','products/1/c_0013.jpg','2015-11-02','努比亚(nubia)My 布拉格 银白 移动联通4G手机 双卡双待【嗨11,下单立减100】金属机身,快速充电!布拉格相机全新体验!');
INSERT INTO `product` VALUES('13','华为 麦芒4','2599','2499','products/1/c_0012.jpg','2015-11-02','华为 麦芒4 晨曦金 全网通版4G手机 双卡双待金属机身 2.5D弧面屏 指纹解锁 光学防抖');
INSERT INTO `product` VALUES('14','vivo X5M','1899','1799','products/1/c_0011.jpg','2015-11-02','vivo X5M 移动4G手机 双卡双待 香槟金【购机送蓝牙耳机+蓝牙自拍杆】5.0英寸大屏显示·八核双卡双待·Hi-Fi移动KTV');
INSERT INTO `product` VALUES('15','Apple iPhone 6 (A1586)','4399','4288','products/1/c_0015.jpg','2015-11-02','Apple iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机长期省才是真的省!点击购机送费版,月月送话费,月月享优惠,畅享4G网络,就在联通4G!');
-
1-1-2 新建一个项目
-
1-1-3 导入jar包
驱动 dbutils c3p0 jstl beanutils
-
1-1-4 包结构
utils工具类:datasourceutils
导入c3p0配置文件
-
1-1-5 新建一个首页index.jsp
添加一个超链接
<body> <h2><a href="${pageContext.request.contextPath }/findAll">展示所有商品</a></h2> </body>
-
1-1-6 点击超链接 发送一个servlet上(FindAllServlet)
-
1-1-7 FindAllServlet
调用service,查询所有的商品 返回值:list
将list放入request域中,请求转发 prodouct_list.jsp/** * 展示所有商品 */ public class FindAllServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.调用service 查询所有商品 返回值list List<Product> plist = null; try { plist = new ProductService().findAll(); } catch (SQLException e) { e.printStackTrace(); } //2.将list放入request域中, request.setAttribute("list", plist); //3.请求转发 request.getRequestDispatcher("/product_list.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
/** * 查询所有商品 * @return list * @throws SQLException */ public List<Product> findAll() throws SQLException { return new ProductDao().findAll(); }
/** * 查询所有商品 * @return * @throws SQLException */ public List<Product> findAll() throws SQLException { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from product"; return qr.query(sql, new BeanListHandler<>(Product.class)); }
<table border="1" align="center" width="88%"> <tr> <th>pid</th> <th>商品图片</th> <th>商品名称</th> <th>市场价</th> <th>商城价</th> <th>商品描述</th> <th>操作</th> </tr> <c:forEach items="${list }" var="p"> <tr> <td width='1%'><input type="checkbox" name="pid" value="${p.pid }"></td> <td width='8%'>${p.pid }</td> <td width='8%'><img alt="" src="${pageContext.request.contextPath }/${p.pimage}" width="80"></td> <td width='8%'>${p.pname }</td> <td width='8%'>${p.market_price }</td> <td width='8%'>${p.shop_price }</td> <td >${p.pdesc }</td> <td width='8%' >修改|删除</td> </tr> </c:forEach> </form> </table>
-
二、案例2-添加商品
需求:
在index.jsp添加一个超链接,跳转到一个页面,用来填写商品信息,点击保存按钮,将商品保存到数据库中
-
2-1 步骤分析
-
2-1-1 在index.jsp添加一个超链接
跳转到add.jsp
<body> <h2><a href="${pageContext.request.contextPath }/findAll">展示所有商品</a></h2> <h2><a href="${pageContext.request.contextPath }/add.jsp">添加商品</a></h2> </body>
-
2-1-2 add.jsp放入一个表单
<form method="post" action="${pageContext.request.contextPath }/addProduct"> <table border="1" align="center" width="40%"> <tr> <td>商品名称</td> <td><input type="text" name="pname"></td> </tr> <tr> <td>市场价</td> <td><input type="text" name="market_price"></td> </tr> <tr> <td>商城价</td> <td><input type="text" name="shop_price"></td> </tr> <tr> <td>商品描述</td> <td><input type="text" name="pdesc"></td> </tr> <tr> <td colspan="2"><input type="submit" value="保存"></td> </tr> </table> </form>
-
2-1-3 表单提交到 AddProductServlet
封装数据
调用service完成保存操作
跳转到FindAllServlet
(请求转发和重定向)/** * 添加商品 */ public class AddProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //0.设置编码 request.setCharacterEncoding("utf-8"); //1.封装数据 Product p=new Product(); try { BeanUtils.populate(p, request.getParameterMap()); //1.1设置pid p.setPid(UUIDUtils.getId()); //1.2设置时间 p.setPdate(new Date()); //2.调用service完成添加操作 new ProductService().addProuct(p); //3.页面跳转 findAll //先用请求转发 request.getRequestDispatcher("/findAll").forward(request, response); } catch (Exception e) { e.printStackTrace(); request.setAttribute("msg", "添加商品失败"); request.getRequestDispatcher("/msg.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class UUIDUtils { /** * 随机生成id * @return */ public static String getId(){ return UUID.randomUUID().toString().replace("-", "").toUpperCase(); }
/** * 添加商品 * @param p * @throws SQLException */ public void addProuct(Product p) throws SQLException { new ProductDao().addProduct(p); }
/** * 添加商品 * @param p * @return * @throws SQLException */ public void addProduct(Product p) throws SQLException { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); /* * `pid` varchar (96), `pname` varchar (150), `market_price` double , `shop_price` double , `