一、商品列表的展示
Web层:
@WebServlet(name = "adminProductList",
urlPatterns = {
"/adminProductList"})
public class adminProductList 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 {
//传递请求到service层
AdminProductService service = new AdminProductService();
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);
}
}
Service层:
public class AdminProductService {
public List<Product> findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
}
Dao层:
public class AdminProductDao {
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;
}
}
Jsp:
<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 }/adminUpdateProductUI?pid=${pro.pid}">
<img src="${pageContext.request.contextPath}/images/i_edit.gif"
border="0" style="CURSOR: hand">
</a></td>
<td align="center" style="HEIGHT: 22px">
<a href="javascript:void(0);" onclick="delProduct('${pro.pid}')">
<img src="${pageContext.request.contextPath}/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
</a>
</td>
</tr>
</c:forEach>
运行结果:
二、添加商品功能
2.1、商品分类添加进来
Jsp页面,当点击了添加按钮之后就讲商品页面添加进来:
<script type="text/javascript">
function addProduct(){
window.location.href = "${pageContext.request.contextPath}/adminAddProductUI";
}
</script>
Web层:
@WebServlet(name = "adminAddProductUI",
urlPatterns = {
"/adminAddProductUI"})
public class adminAddProductUI 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<Category> categoryList = null;
try {
categoryList = service.findAllCategory();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("categoryList", categoryList);
request.getRequestDispatcher("/admin/product/add.jsp").forward(request, response);
}
}
Service层:
public class AdminProductService {
public List<Category> findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
}
Dao层:
publ