jsp-servlet 商品显示的实现
1实现原理
2代码实现(部分)
AdminProductListServlet.java
@WebServlet(name = "AdminProductListServlet", urlPatterns = {"/adminProductList"}) public class AdminProductListServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AdminProductService service = new AdminProductService(); List<Product> productList = null; try { productList = service.findAllProduct(); } catch (SQLException e) { e.printStackTrace(); } request.setAttribute("productList",productList); request.getRequestDispatcher("/admin/product/list.jsp").forward(request,response); } }
AdminProductService.java
public List<Product> findAllProduct() throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.findAllProduct(); }
AdminProductDao.java
public List<Product> findAllProduct() throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from product"; List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class)); return productList; }
list.jsp
d.add('010401','0104','商品管理','${pageContext.request.contextPath}/adminProductList','','mainFrame');
<c:forEach items="${productList}" var="pro" 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}/${pro.pimage}"></td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"> ${pro.pname} </td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"> ${pro.shop_price} </td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%"> ${pro.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>