JSP第十三次课:购物车设计
相关知识:购物车原理
(1)购物车里边存储的是商品链表,实现购物车的添加商品,删除商品,修改商品数量,清除购物车
(2)detail.jsp详细信息页,包含文本框输入数量,点击购买,调用doCar.jsp?action=add,实现购买商品
(3)showCar.jsp页显示购物车,可以进行购物车商品数量修改、删除商品,清除购物车,统计金额等操作
(4)关于购物车“添加商品”--购买,删除,修改商品数量,清除购物车等操作,需要doCar页进行统一处理
(5)doCar.jsp用用于获得用户页面(View试图)提交的信息,觉得进行那种操作(购买,删除,修改商品数量),并返回相关界面
一、购物车Bean设计ShopCar.java
package mybean;
import java.util.LinkedList;
public class ShopCar {
//list.clear();list.size();list.add();list.get(i);list.remove(i)
private LinkedList<Goods> shops=new LinkedList<Goods>();
public int check(int gid){
//查看购物车中是否有该商品,确定位置
int flag=-1;
for(int i=0;i<shops.size();i++){
if(shops.get(i).getGid()==gid){
flag=i;
break;
}
}
return flag;
}
public void add(Goods g){
int n=check(g.getGid());
if(n==-1){
shops.add(g);
}else{
Goods g1=shops.get(n);
g1.setNum(shops.get(n).getNum()+g.getNum());
}
}
public void delete(int gid){
//删除商品需要知道删除那个商品gid
int n=check(gid);
shops.remove(n);
}
public void update(int gid,int num){
//修改商品数量,只需要知道商品ID, 需要购买的数量
int n=check(gid);
if(num==0){
delete(gid);
}else{
Goods g1=shops.get(n);
g1.setNum(num);
}
}
public void clear(){
this.shops.clear();
}
public LinkedList<Goods> getShops() {
return shops;
}
}
二、购物车相关操作设计doCar.jsp(Servlet思想)
添加商品操作
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="utf-8"%>
<%
ShopCar myCar=(ShopCar)session.getAttribute("myCar");
request.setCharacterEncoding("utf-8");
if(myCar==null){
myCar=new ShopCar();
session.setAttribute("myCar", myCar);
}
String action=request.getParameter("action");
if("buy".equals(action)){
int gid=Integer.parseInt(request.getParameter("gid"));
String name=request.getParameter("name");
float price=Float.parseFloat(request.getParameter("price"));
int num=Integer.parseInt(request.getParameter("num"));
Goods g=new Goods();
g.setGid(gid);
g.setName(name);
g.setPrice(price);
g.setNum(num);
myCar.add(g);
response.sendRedirect("index.jsp");
}
%>
2、其他操作
if("clear".equals(action)){
myCar.clear();
response.sendRedirect("index.jsp");
}
if("update".equals(action)){
int gid=Integer.parseInt(request.getParameter("gid"));
int num=Integer.parseInt(request.getParameter("num"));
myCar.update(gid, num);
response.sendRedirect("showCar.jsp");
}
if("delete".equals(action)){
int gid=Integer.parseInt(request.getParameter("gid"));
myCar.delete(gid);
response.sendRedirect("showCar.jsp");
}
三、购物车页面设计
1、初始页面(静态的,可复制使用):showCar.jsp
<html><head>
<title>My JSP 'viewShop.jsp' starting page</title>
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="content">
<table border="1" width="450">
<tbody><tr height="50">
<td colspan="6" align="center"><p>该用户没有登录!!<br>购买的商品如下</p></td>
</tr>
<tr align="center" bgcolor="lightgrey" height="30">
<td>ID</td>
<td width="25%">名称</td>
<td>单价</td>
<td>数量</td>
<td>总价(元)</td>
<td>移除(-1/次)</td>
</tr>
<tr align="center" height="50">
<td>10002</td>
<td>小米 M4 </td>
<td>2117</td>
<td>1</td>
<td>2117</td>
<td>
<a href="doCar.jsp?action=remove&gid=10002">移除</a>
</td>
</tr>
<tr align="center" height="50">
<td>10003</td>
<td>魅族 MX4 </td>
<td>1369</td>
<td>2</td>
<td>2738</td>
<td>
<a href="doCar.jsp?action=remove&gid=10003">移除</a>
</td>
</tr>
<tr align="center" height="50"><td colspan="6">应付金额:4855.0</td></tr>
<tr align="center" height="50">
<td colspan="3"><a href="showGoods.jsp">继续购物</a></td>
<td colspan="3"><a href="doCar.jsp?action=clear">清空购物车</a></td>
</tr>
</tbody></table>
</div></body></html>
2、修改为动态页面showCar.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'viewShop.jsp' starting page</title>
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<%
User user=(User)session.getAttribute("user");
String name="无名氏";
if(user!=null)
{
name=user.getName();
}
%>
<div class="content">
<table border="1" width="450">
<tbody><tr height="50">
<td colspan="6" align="center"><p><%=name %><br>购买的商品如下</p></td>
</tr>
<tr align="center" bgcolor="lightgrey" height="30">
<td>ID</td>
<td width="25%">名称</td>
<td>单价</td>
<td>数量</td>
<td>总价(元)</td>
<td>删除</td>
</tr>
<%
ShopCar myCar=(ShopCar)session.getAttribute("myCar");
float sum=0;
LinkedList<Goods> gs=myCar.getShops();
if(gs.size()>0){
for(Goods g:gs){
sum+=g.getPrice()*g.getNum();
%>
<tr align="center" height="50">
<td><%=g.getGid() %></td>
<td><%=g.getName() %> </td>
<td><%=g.getPrice() %></td>
<td>
<form method="Post" action="doCar.jsp?action=update&gid=<%=g.getGid()%>">
<input type="text" name="num" size="1"value="<%=g.getNum() %>">
<input type="submit" value="修改"/>
</form>
</td>
<td><%=g.getPrice()*g.getNum()%></td>
<td>
<a href="doCar.jsp?action=delete&gid=<%=g.getGid()%>">删除</a>
</td>
</tr>
<%
} //endfor
}else{
%>
<tr align="center" height="50">
<td colspan="6">无商品:</td></tr>
<%} %>
<tr align="center" height="50">
<td colspan="6">应付金额:<%=sum%></td></tr>
<tr align="center" height="50">
<td colspan="3"><a href="index.jsp">继续购物</a></td>
<td colspan="3"><a href="doCar.jsp?action=clear">清空购物车</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
四、前台购物页面操作
1、right.jsp
<li>名称:<a href="detail.jsp?gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
2、detail.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<%
String gid=request.getParameter("gid");
GoodsDAO dao=new GoodsDAO();
Goods g=dao.getById(Integer.parseInt(gid));
%>
<div class="detail">
<form method="POST" action="doCar.jsp">
<img src="<%=g.getPicture()%>">
<ul>
<li>编号:<%=g.getGid() %></li>
<li>名称:<%=g.getName() %></li>
<li>价格:<%=g.getPrice() %></li>
<li><a href="comment.jsp?gid=<%=g.getGid()%>">查看评价</a></li>
<li>数量:<input name="num" size="4" type="text">
<input value="购买" type="submit"></li>
<input name="gid" value="<%=g.getGid()%>" type="hidden">
<input name="name" value="<%=g.getName()%>" type="hidden">
<input name="price" value="<%=g.getPrice()%>" type="hidden">
<input name="action" value="buy" type="hidden">
</ul>
</form>
</div>
<div class="describe">商品描述:<%=g.getContent()%></div>
转载于:https://blog.51cto.com/dyzyxy/1625839