一、创建ListProductServlet
package cn.itcast.itcaststore.web.servlet.manager;
/**
* 后台系统
* 查询所有商品信息的servlet
*/
public class ListProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建service层的对象
ProductService service = new ProductService();
try {
// 2.调用service层用于查询所有商品的方法
List<Product> ps = service.listAll();
// 3.将查询出的所有商品放进request域中
request.setAttribute("ps", ps);
// 4.重定向到list.jsp页面
request.getRequestDispatcher("/admin/products/list.jsp").forward(
request, response);
return;
} catch (ListProductException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
}
}
二、创建ProductService,编写查询所有商品方法
package cn.itcast.itcaststore.service;
public class ProductService {
private ProductDao dao = new ProductDao();
// 查找所有商品信息
public List<Product> listAll() throws ListProductException {
try {
return dao.listAll();
} catch (SQLException e) {
e.printStackTrace();
throw new ListProductException("查询商品失败");
}
}
}
三、创建ProductDao,编写查询所有商品方法
package cn.itcast.itcaststore.dao;
public class ProductDao {
// 查找所有商品
public List<Product> listAll() throws SQLException {
String sql = "select * from products";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanListHandler<Product>(Product.class));
}
}
四、实现页面展示
/itcaststore/WebContent/admin/products/list.jsp中显示商品列表的代码
<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="24%">商品编号</td>
<td align="center" width="18%">商品名称</td>
<td align="center" width="9%">商品价格</td>
<td align="center" width="9%">商品数量</td>
<td width="8%" align="center">商品类别</td>
<td width="8%" align="center">编辑</td>
<td width="8%" align="center">删除</td>
</tr>
<!-- 循环输出所有商品 -->
<c:forEach items="${ps}" var="p">
<tr onmouseover="this.style.backgroundColor = 'white'"
onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="200">${p.id }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${p.name }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="8%">${p.price }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="8%">${p.pnum }</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center">${p.category}</td>
<td align="center" style="HEIGHT: 22px" width="7%">
<a href="${pageContext.request.contextPath}/findProductById?id=${p.id}&type=admin">
<img src="${pageContext.request.contextPath}/admin/images/i_edit.gif" border="0" style="CURSOR: hand">
</a>
</td>
<td align="center" style="HEIGHT: 22px" width="7%">
<a href="${pageContext.request.contextPath}/deleteProduct?id=${p.id}" onclick="javascript:return p_del()">
<img src="${pageContext.request.contextPath}/admin/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</c:forEach>
</table>