JSP第十三次課:購物車設計css
相關知識:購物車原理html
(1)購物車裏邊存儲的是商品鏈表,實現購物車的添加商品,刪除商品,修改商品數量,清除購物車java
(2)detail.jsp詳細信息頁,包含文本框輸入數量,點擊購買,調用doCar.jsp?action=add,實現購買商品session
(3)showCar.jsp頁顯示購物車,能夠進行購物車商品數量修改、刪除商品,清除購物車,統計金額等操做jsp
(4)關於購物車「添加商品」--購買,刪除,修改商品數量,清除購物車等操做,須要doCar頁進行統一處理ide
(5)doCar.jsp用用於得到用戶頁面(View試圖)提交的信息,以爲進行那種操做(購買,刪除,修改商品數量),並返回相關界面this
1、購物車Bean設計ShopCar.javaspa
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){orm
//刪除商品須要知道刪除那個商品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;
}
}
2、購物車相關操做設計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");
}
%>
二、其餘操做
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");
}
3、購物車頁面設計
一、初始頁面(靜態的,可複製使用):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>
二、修改成動態頁面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>
4、前臺購物頁面操做
一、right.jsp
<li>名稱:<a href="detail.jsp?gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
二、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>