案例1-后台商品列表的展示
1 修改left.jsp里面的代码
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>菜单</title> <link href="${pageContext.request.contextPath}/css/left.css" rel="stylesheet" type="text/css"/> <link rel="StyleSheet" href="${pageContext.request.contextPath}/css/dtree.css" type="text/css" /> </head> <body> <table width="100" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="12"></td> </tr> </table> <table width="100%" border="0"> <tr> <td> <div class="dtree"> <a href="javascript: d.openAll();">展开所有</a> | <a href="javascript: d.closeAll();">关闭所有</a> <script type="text/javascript" src="${pageContext.request.contextPath}/js/dtree.js"></script> <script type="text/javascript"> d = new dTree('d'); d.add('01',-1,'系统菜单树'); //01代表本级节点的编号 -1代表根节点 d.add('0102','01','分类管理','','','mainFrame'); //0102代表本级节点的编号 01代表父级节点 d.add('010201','0102','分类管理','${pageContext.request.contextPath}/admin/category/list.jsp','','mainFrame'); d.add('0104','01','商品管理'); d.add('010401','0104','商品管理','${pageContext.request.contextPath}/adminProductList','','mainFrame'); d.add('0105','01','人事管理'); d.add('010501','0105','人事管理','${pageContext.request.contextPath}/admin/product/list.jsp','','mainFrame'); document.write(d); </script> </div> </td> </tr> </table> </body> </html>
2 web层的AdminProductListServlet
package www.test.web; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import www.test.domain.Product; import www.test.service.AdminProductService; public class AdminProductListServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 传递请求到service层 AdminProductService service = new AdminProductService(); //调用service层的方法 List<Product> productList = null; try { productList = service.findAllProduct(); } catch (SQLException e) { e.printStackTrace(); } // 将productList放到request域 request.setAttribute("productList", productList); request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 service层的AdminProductService
package www.test.service; import java.sql.SQLException; import java.util.List; import www.test.dao.AdminProductDao; import www.test.domain.Product; public class AdminProductService { public List<Product> findAllProduct() throws SQLException { //因为没有复杂业务 直接传递请求到dao层 AdminProductDao dao = new AdminProductDao(); return dao.findAllProduct(); } }
4 dao层AdminProductDao
package www.test.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import www.test.domain.Product; import www.test.utils.C3P0Utils; public class AdminProductDao { public List<Product> findAllProduct() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; List<Product> productList = qr.query(sql, new BeanListHandler<Product>(Product.class)); return productList; } }
5 admin/product/ list.jsp代码
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <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" rel="stylesheet" type="text/css" /> <script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script> <script type="text/javascript"> function addProduct(){ window.location.href = "${pageContext.request.contextPath}/admin/product/add.jsp"; } </script> </HEAD> <body> <br> <form id="Form1" name="Form1" action="${pageContext.request.contextPath}/user/list.jsp" method="post"> <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="addProduct()"> 添加</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 align="center" width="17%">商品名称</td> <td align="center" width="17%">商品价格</td> <td align="center" width="17%">是否热门</td> <td width="7%" align="center">编辑</td> <td width="7%" align="center">删除</td> </tr> <!-- varStatus 记录第几次遍历 --> <c:forEach items="${productList }" var="product" varStatus="vs"> <tr onmouseover="this.style.backgroundColor = 'white'" onmouseout="this.style.backgroundColor = '#F5FAFE';"> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${vs.count }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"><img width="40" height="45" src="${pageContext.request.contextPath }/${product.pimage }"></td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.pname }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.shop_price }</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"> ${product.is_hot==1?"是":"否" }</td> <td align="center" style="HEIGHT: 22px"><a href="${ pageContext.request.contextPath }/admin/product/edit.jsp"> <img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand"> </a></td> <td align="center" style="HEIGHT: 22px"><a href="#"> <img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand"> </a></td> </tr> </c:forEach> </table> </td> </tr> </TBODY> </table> </form> </body> </HTML>