POJO嵌套之简单应用----购物车

来源:互联网 发布:淘宝退货率高怎么办 编辑:程序博客网 时间:2024/06/18 14:58

JSP开发------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Buy.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.shopping.client.*, com.bjsxt.shopping.product.*" %>
<%
Cart c = (Cart)session.getAttribute("cart");
if(c == null) {
c = new Cart();
session.setAttribute("cart", c);
}
%>
<!-- 同上 -->
<%-- <<jsp:useBean id="c" class="com.bjsxt.shopping.client.Cart" scope="session"></jsp:useBean>
 --%>
<%
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
if(action != null && action.trim().equals("add")) {
int id = Integer.parseInt(request.getParameter("id"));
Product p = ProductMgr.getInstance().loadById(id);
CartItem ci = new CartItem();
ci.setProduct(p);
ci.setCount(1);
c.add(ci);
}


if(action != null && action.trim().equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
c.deleteItemById(id);
}


if(action != null && action.trim().equals("update")) {
for(int i=0; i<c.getItems().size(); i++) {
CartItem ci = c.getItems().get(i);
int count = Integer.parseInt(request.getParameter("p" + ci.getProduct().getId()));
ci.setCount(count);
}
}
 %> 


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<%
List<CartItem> items = c.getItems();
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>购物车</title>


</head>
<body>




<form action="Buy.jsp" method="get">
<input type="hidden" name="action" value="update"/>
<table align="center" border="1">
<tr>
<td>产品ID</td>
<td>产品名称</td>
<td>购买数量</td>
<td>单价</td>
<td>总价</td>
<td>处理</td>
</tr>
<%
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem ci = it.next();
%>
<tr>
<td><%=ci.getProduct().getId() %></td>
<td><%=ci.getProduct().getName() %></td>
<td>
<input type="text" size=3 name="<%="p" + ci.getProduct().getId() %>" value="<%=ci.getCount() %>">
</td>
<td><%=ci.getProduct().getNormalPrice() %></td>
<td><%=ci.getProduct().getNormalPrice()*ci.getCount() %></td>
<td>

<a href="Buy.jsp?action=delete&id=<%=ci.getProduct().getId() %>">删除</a>

</td>
</tr>
<%
}
%>
<tr>
<td colspan=6>
<a href="Confirm.jsp">下单</a>
<a href="javascript:document.forms[0].submit()">修改</a>
</td>
</tr>
</table>
</form>
</body>
</html>


Confirm.jsp


<%@ page language="java" contentType="text/html; charset=GB18030" import="java.util.*"
pageEncoding="GB18030"%>

<%@ page import="com.bjsxt.shopping.product.*, com.bjsxt.shopping.user.*, com.bjsxt.shopping.client.*" %>




<%
User u = (User)session.getAttribute("user");
if(u == null) {
response.sendRedirect("UserLogin.jsp");
return;
}


Cart c = (Cart)session.getAttribute("cart");
if(c == null) {
c = new Cart();
session.setAttribute("cart", c);
}
%>




<%
List<CartItem> items = c.getItems();
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>最后确认</title>


</head>
<body>


<table align="center" border="1">
<tr>
<td>产品ID</td>
<td>产品名称</td>
<td>购买数量</td>
<td>单价</td>
<td>总价</td>


</tr>
<%
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem ci = it.next();
%>
<tr>
<td><%=ci.getProduct().getId() %></td>
<td><%=ci.getProduct().getName() %></td>
<td><%=ci.getCount() %></td>
<td><%=ci.getProduct().getMemberPrice() %></td>
<td><%=ci.getProduct().getMemberPrice() * ci.getCount() %></td>

</tr>
<%
}
%>
<tr>
<td colspan=4>

</td>
<td>
总价: <%=c.getTotalMemberPrice()%>
</td>
</tr>
<tr>
<td colspan=5>
<form action="Order.jsp" method="post">
欢迎你: <%=u.getUsername() %> <br>
phone: <%=u.getPhone() %> <br>
Addr : <textarea name="addr"><%=u.getAddr() %></textarea>
<input type="submit" value="确认下单"> 
</form>
</td>
</tr>
</table>


</body>
</html>


CartItem.java


package com.bjsxt.shopping.client;


import com.bjsxt.shopping.product.Product;


public class CartItem {
//productid
private Product product;
private int count;


public int getCount() {
return count;
}


public void setCount(int count) {
this.count = count;
}


public Product getProduct() {
return product;
}


public void setProduct(Product product) {
this.product = product;
}
}


Cart.java


package com.bjsxt.shopping.client;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Cart {
List<CartItem> items = new ArrayList<CartItem>();


public List<CartItem> getItems() {
return items;
}


public void setItems(List<CartItem> items) {
this.items = items;
}

public void add(CartItem ci) {
for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
CartItem item = iter.next();
if(item.getProduct().getId() == ci.getProduct().getId()) {
item.setCount(item.getCount() + 1);
return;
}
}

items.add(ci);
}

public double getTotalMemberPrice() {
double d = 0.0;
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem current = it.next();
d += current.getProduct().getMemberPrice() * current.getCount();
}
return d;
}

public void deleteItemById(int productId) {
for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
CartItem item = iter.next();
if(item.getProduct().getId() == productId) {
iter.remove();
}
}
}
}



===================================================================================


以上来源于尚学堂马士兵老师的视频案例


实际中大多将购物车作为单独的一张表保存在数据库中,而保存在session中比较简单,个人选择。