求ssh2 项目代码(特别是一定要有购物车,或者单纯的购物车和jsp也可以参考)
谢谢,给我一个好一点的,困扰了我两天的了,这个小小的财富值就送你了!我今天做的用session.put()这个方法来保存,不知道为什么,只能每次就是购物车里面总是只出现一...
谢谢,给我一个好一点的,困扰了我两天的了,这个小小的财富值就送你了!
我今天做的 用session.put()这个方法来保存,不知道为什么,只能每次就是购物车里面总是只出现一个我最近一次点的商品而已,前面的没有保存,不知道哪里出现问题,也不知道怎么样继续添加保存,达到真正的购物车功能! 展开
我今天做的 用session.put()这个方法来保存,不知道为什么,只能每次就是购物车里面总是只出现一个我最近一次点的商品而已,前面的没有保存,不知道哪里出现问题,也不知道怎么样继续添加保存,达到真正的购物车功能! 展开
2个回答
展开全部
我以前做过,很全的一个!你找下你需要的代码吧,
//发票集合 id
public static int receiptids;
//购物车集合Linked
public static Map<String ,Goodslist> ShoppingMap =null;
//获取集合
public Map<String, Goodslist> getShoppingMap() {
if(ShoppingMap==null){
//System.out.println("get==null");
return ShoppingMap=new HashMap<String,Goodslist>();
}else{
return ShoppingMap;
}
}
public void setShoppingMap(Map<String, Goodslist> shoppingMap) {
ShoppingMap = shoppingMap;
}
//添加商品
public void addBookMap(Goodslist shoop){
if(ShoppingMap==null){
ShoppingMap=new HashMap<String,Goodslist>();
ShoppingMap.put(shoop.getGname(),shoop);
}else{
this.ShoppingMap.put(shoop.getGname(),shoop);
}
System.out.println(ShoppingMap.size());
}
//删除集合中的key
public void remBookMap(String key){
if(this.ShoppingMap.containsKey(key)){
this.ShoppingMap.remove(key);
}
}
//获取商品集合
public Goodslist getBookMap(String key){
return (Goodslist)this.ShoppingMap.get(key);
}
//购物车管理
public Map<String ,Goodslist> ManagerAddShopping(Goodslist goodslist){
//添加购商品到购物车
String goodsName=goodslist.getGname();
if(goodslist!=null){
if(this.getShoppingMap().containsKey(goodsName)){
//得得单个商品数量 //如果有则加一
int count=ShoppingMap.get(goodsName).getGnumber();
ShoppingMap.get(goodsName).setGnumber(count+1);
//得到单个商品总价
ShoppingMap.get(goodsName).setSubtotal(goodslist.getPrice()*(count+1));
}else{
System.out.println("新添加商品到购物车");
//添加一个商品集合到购物车
this.addBookMap(goodslist);
}
return ShoppingMap;
}
return null;
}
//修改数量
public void updateShopping(String key,int number){
System.out.println("key"+key+number);
if(this.getShoppingMap().containsKey(key)){
//加
if(number>0){
//得得单个商品数量 //如果有则加一
//int count=ShoppingMap.get(key).getGnumber();
ShoppingMap.get(key).setGnumber(number);
//得到单个商品总价
ShoppingMap.get(key).setSubtotal(ShoppingMap.get(key).getPrice()*(number));
}
}
}
//计价
public double getSumPirce(){
double sumMoney=0;
if(this.ShoppingMap.size()>0){
for(Map.Entry<String, Goodslist> entry : ShoppingMap.entrySet()) {
System.out.println(entry.getKey());
sumMoney=sumMoney+ShoppingMap.get(entry.getKey()).getSubtotal();
}
/*Iterator it =ShoppingMap.keySet().iterator();
while(it.hasNext()){
String key=(String)it.next();
}
*/
}
return sumMoney;
}
//模糊搜索
public List getSerch(String name){
List list=null;
if(name.isEmpty()){
System.out.println("Manager.getSerch() return null");
return null;
}else{
//list=imples.getSeachResults(name);
return list;
}
}
/*
*读取 订单人的收货地址 根据会员id
*/
public List<Memberdatas> getConsigneeInfo(int id){
String sql="from Memberdatas where MId="+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*读取 订单人的收货地址 根据编号
*/
public List<Memberdatas> getConsigneeInfoID(int id){
String sql="from Memberdatas where miId="+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*读取 订单人的商品集合
*/
public List<Goods> getConsigneeGoods(){
return null;
}
/*
*读取 订单人的发票信息
*/
public List<Receipt> getReceipt(int meberID){
String sql="from Receipt where OId="+meberID;
return ExcuteSQL.executQuery(sql, null);
}
/*
*插入 订单人的收货地址
*/
public boolean inisertConsigneeInfo(Memberdatas memberdatas){
boolean b=ExcuteSQL.save(memberdatas);
return b;
}
//删除收货人地址
public boolean delectMebreAddes(int mid){
String sql="delete Memberdatas where miId="+mid;
//System.out.println("ManagerOrder.delectMebreAddes()="+mid);
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//修改收货人地址
public boolean updateAdders(int mlid,String [] whe){
//String sql="update Memberdatas set names=?,adders=?, phone=? , phones=? where miId="+mlid;
String sql="update Memberdatas set names='"+whe[0]+"',adders='"+whe[1]+"', phone='"+whe[2]+"', phones='"+whe[3]+"' where miId="+mlid;
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//修改发票
public boolean updateReli(int ids,String rname, String rtype){
String sql="update Receipt set rtype='"+rtype+"' ,retop='"+rname+"' where ids="+ids;
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//添加发票
public boolean insertReli(Receipt re){
return ExcuteSQL.save(re);
}
//插入商品列表
public boolean insertGoodsList(Goodslist golist)
{
return ExcuteSQL.save(golist);
}
//临时商品集合 从购物车中取出的商品
public static List<Goodslist> TempList;//=new ArrayList<Goodslist>();
public void addTempList(Goodslist go){
if(TempList==null){
TempList=new ArrayList<Goodslist>();
}
TempList.add(go);
}
public List<Goodslist> getTempList() {
if(TempList==null){
return TempList=new ArrayList<Goodslist>();
}else{
return TempList;
}
}
public void setTempList(List<Goodslist> tempList) {
TempList = tempList;
}
public List<Receipt> reList;
public List<Receipt> getReList() {
if(reList==null){
return reList=new ArrayList<Receipt>();
}else{
return reList;
}
}
public void setReList(List<Receipt> reList) {
if(reList==null){
reList=new ArrayList<Receipt>();
}
this.reList = reList;
}
public void addreList(Receipt re){
if(reList==null){
reList=new ArrayList<Receipt>();
}
reList.add(re);
}
//查询订单根据会员id
public List<GoodsOrader> getOrder(int mid){
//先获取订单id
List<GoodsOrader> listRetu=new ArrayList<GoodsOrader>();
Interfaces imp=new Imple();
List ids=imp.getOrderid(mid);
System.out.println("ManagerOrder.getOrder()"+ids);
if(ids!=null){
for(int i=0;i<ids.size();i++){
System.out.println("ManagerOrder.getOrder()ids.size()="+ids.size());
String sql="from GoodsOrader where orders.OId="+Integer.parseInt(ids.get(i).toString());
List<GoodsOrader> list=ExcuteSQL.executQuery(sql, null);
for(GoodsOrader god:list){
//添加订单对象,
listRetu.add(god);
}
}
return listRetu;
}else{
return null;
}
}
//删除订单
public boolean deleteOrder(int oid){
String sql="delete GoodsOrader where orders.OId="+oid;
int b=ExcuteSQL.executUpdate(sql, null);
if(b>0) return true;else return false;
}
}
-------------------------------------------------------------------------
package snail.su.rui.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import snail.su.manager.ManagerOrder;
import snail.su.vo.Goodslist;
public class UpdateShoop extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//修改购物车
String shoopKey=request.getParameter("shoopKey");
String number=request.getParameter("number");
String shookeys=new String(shoopKey.getBytes("ISO-8859-1"),"gb2312");
ManagerOrder order=new ManagerOrder();
order.updateShopping(shookeys, Integer.parseInt(number));
request.getSession().setAttribute("buyNumber",order.ShoppingMap.size());
//计价
double sunMoney=order.getSumPirce();
out.print(sunMoney);
System.out.println("UpdateShoop.doPost()"+sunMoney);
}
}
-----------------------------------------------------
//删除购物车里的物品
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String key=request.getParameter("key");
key=new String(key.getBytes("ISO-8859-1"),"utf-8");
ManagerOrder orader=new ManagerOrder();
orader.remBookMap(key);
request.getSession().setAttribute("buyNumber",orader.ShoppingMap.size());
//获取商品集合到购物车
ManagerOrder order =new ManagerOrder();
Map<String ,Goodslist> shoopMap=order.getShoppingMap();
//添加集合到session
HttpSession ShoopSession=request.getSession();
ShoopSession.setAttribute("SUshoopMap",shoopMap);
System.out.println(shoopMap.size());
//把session写入到cookie里面
if(ShoopSession!=null){
System.out.println("写入session到了cookie了!");
Cookie cookie=new Cookie("JSESSIONID",ShoopSession.getId());
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
request.getRequestDispatcher("WEB-INF/rui/myShoop.jsp").forward(request, response);
}
//发票集合 id
public static int receiptids;
//购物车集合Linked
public static Map<String ,Goodslist> ShoppingMap =null;
//获取集合
public Map<String, Goodslist> getShoppingMap() {
if(ShoppingMap==null){
//System.out.println("get==null");
return ShoppingMap=new HashMap<String,Goodslist>();
}else{
return ShoppingMap;
}
}
public void setShoppingMap(Map<String, Goodslist> shoppingMap) {
ShoppingMap = shoppingMap;
}
//添加商品
public void addBookMap(Goodslist shoop){
if(ShoppingMap==null){
ShoppingMap=new HashMap<String,Goodslist>();
ShoppingMap.put(shoop.getGname(),shoop);
}else{
this.ShoppingMap.put(shoop.getGname(),shoop);
}
System.out.println(ShoppingMap.size());
}
//删除集合中的key
public void remBookMap(String key){
if(this.ShoppingMap.containsKey(key)){
this.ShoppingMap.remove(key);
}
}
//获取商品集合
public Goodslist getBookMap(String key){
return (Goodslist)this.ShoppingMap.get(key);
}
//购物车管理
public Map<String ,Goodslist> ManagerAddShopping(Goodslist goodslist){
//添加购商品到购物车
String goodsName=goodslist.getGname();
if(goodslist!=null){
if(this.getShoppingMap().containsKey(goodsName)){
//得得单个商品数量 //如果有则加一
int count=ShoppingMap.get(goodsName).getGnumber();
ShoppingMap.get(goodsName).setGnumber(count+1);
//得到单个商品总价
ShoppingMap.get(goodsName).setSubtotal(goodslist.getPrice()*(count+1));
}else{
System.out.println("新添加商品到购物车");
//添加一个商品集合到购物车
this.addBookMap(goodslist);
}
return ShoppingMap;
}
return null;
}
//修改数量
public void updateShopping(String key,int number){
System.out.println("key"+key+number);
if(this.getShoppingMap().containsKey(key)){
//加
if(number>0){
//得得单个商品数量 //如果有则加一
//int count=ShoppingMap.get(key).getGnumber();
ShoppingMap.get(key).setGnumber(number);
//得到单个商品总价
ShoppingMap.get(key).setSubtotal(ShoppingMap.get(key).getPrice()*(number));
}
}
}
//计价
public double getSumPirce(){
double sumMoney=0;
if(this.ShoppingMap.size()>0){
for(Map.Entry<String, Goodslist> entry : ShoppingMap.entrySet()) {
System.out.println(entry.getKey());
sumMoney=sumMoney+ShoppingMap.get(entry.getKey()).getSubtotal();
}
/*Iterator it =ShoppingMap.keySet().iterator();
while(it.hasNext()){
String key=(String)it.next();
}
*/
}
return sumMoney;
}
//模糊搜索
public List getSerch(String name){
List list=null;
if(name.isEmpty()){
System.out.println("Manager.getSerch() return null");
return null;
}else{
//list=imples.getSeachResults(name);
return list;
}
}
/*
*读取 订单人的收货地址 根据会员id
*/
public List<Memberdatas> getConsigneeInfo(int id){
String sql="from Memberdatas where MId="+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*读取 订单人的收货地址 根据编号
*/
public List<Memberdatas> getConsigneeInfoID(int id){
String sql="from Memberdatas where miId="+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*读取 订单人的商品集合
*/
public List<Goods> getConsigneeGoods(){
return null;
}
/*
*读取 订单人的发票信息
*/
public List<Receipt> getReceipt(int meberID){
String sql="from Receipt where OId="+meberID;
return ExcuteSQL.executQuery(sql, null);
}
/*
*插入 订单人的收货地址
*/
public boolean inisertConsigneeInfo(Memberdatas memberdatas){
boolean b=ExcuteSQL.save(memberdatas);
return b;
}
//删除收货人地址
public boolean delectMebreAddes(int mid){
String sql="delete Memberdatas where miId="+mid;
//System.out.println("ManagerOrder.delectMebreAddes()="+mid);
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//修改收货人地址
public boolean updateAdders(int mlid,String [] whe){
//String sql="update Memberdatas set names=?,adders=?, phone=? , phones=? where miId="+mlid;
String sql="update Memberdatas set names='"+whe[0]+"',adders='"+whe[1]+"', phone='"+whe[2]+"', phones='"+whe[3]+"' where miId="+mlid;
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//修改发票
public boolean updateReli(int ids,String rname, String rtype){
String sql="update Receipt set rtype='"+rtype+"' ,retop='"+rname+"' where ids="+ids;
int count=ExcuteSQL.executUpdate(sql, null);
if(count>0){return true;}else{ return false;}
}
//添加发票
public boolean insertReli(Receipt re){
return ExcuteSQL.save(re);
}
//插入商品列表
public boolean insertGoodsList(Goodslist golist)
{
return ExcuteSQL.save(golist);
}
//临时商品集合 从购物车中取出的商品
public static List<Goodslist> TempList;//=new ArrayList<Goodslist>();
public void addTempList(Goodslist go){
if(TempList==null){
TempList=new ArrayList<Goodslist>();
}
TempList.add(go);
}
public List<Goodslist> getTempList() {
if(TempList==null){
return TempList=new ArrayList<Goodslist>();
}else{
return TempList;
}
}
public void setTempList(List<Goodslist> tempList) {
TempList = tempList;
}
public List<Receipt> reList;
public List<Receipt> getReList() {
if(reList==null){
return reList=new ArrayList<Receipt>();
}else{
return reList;
}
}
public void setReList(List<Receipt> reList) {
if(reList==null){
reList=new ArrayList<Receipt>();
}
this.reList = reList;
}
public void addreList(Receipt re){
if(reList==null){
reList=new ArrayList<Receipt>();
}
reList.add(re);
}
//查询订单根据会员id
public List<GoodsOrader> getOrder(int mid){
//先获取订单id
List<GoodsOrader> listRetu=new ArrayList<GoodsOrader>();
Interfaces imp=new Imple();
List ids=imp.getOrderid(mid);
System.out.println("ManagerOrder.getOrder()"+ids);
if(ids!=null){
for(int i=0;i<ids.size();i++){
System.out.println("ManagerOrder.getOrder()ids.size()="+ids.size());
String sql="from GoodsOrader where orders.OId="+Integer.parseInt(ids.get(i).toString());
List<GoodsOrader> list=ExcuteSQL.executQuery(sql, null);
for(GoodsOrader god:list){
//添加订单对象,
listRetu.add(god);
}
}
return listRetu;
}else{
return null;
}
}
//删除订单
public boolean deleteOrder(int oid){
String sql="delete GoodsOrader where orders.OId="+oid;
int b=ExcuteSQL.executUpdate(sql, null);
if(b>0) return true;else return false;
}
}
-------------------------------------------------------------------------
package snail.su.rui.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import snail.su.manager.ManagerOrder;
import snail.su.vo.Goodslist;
public class UpdateShoop extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//修改购物车
String shoopKey=request.getParameter("shoopKey");
String number=request.getParameter("number");
String shookeys=new String(shoopKey.getBytes("ISO-8859-1"),"gb2312");
ManagerOrder order=new ManagerOrder();
order.updateShopping(shookeys, Integer.parseInt(number));
request.getSession().setAttribute("buyNumber",order.ShoppingMap.size());
//计价
double sunMoney=order.getSumPirce();
out.print(sunMoney);
System.out.println("UpdateShoop.doPost()"+sunMoney);
}
}
-----------------------------------------------------
//删除购物车里的物品
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String key=request.getParameter("key");
key=new String(key.getBytes("ISO-8859-1"),"utf-8");
ManagerOrder orader=new ManagerOrder();
orader.remBookMap(key);
request.getSession().setAttribute("buyNumber",orader.ShoppingMap.size());
//获取商品集合到购物车
ManagerOrder order =new ManagerOrder();
Map<String ,Goodslist> shoopMap=order.getShoppingMap();
//添加集合到session
HttpSession ShoopSession=request.getSession();
ShoopSession.setAttribute("SUshoopMap",shoopMap);
System.out.println(shoopMap.size());
//把session写入到cookie里面
if(ShoopSession!=null){
System.out.println("写入session到了cookie了!");
Cookie cookie=new Cookie("JSESSIONID",ShoopSession.getId());
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
request.getRequestDispatcher("WEB-INF/rui/myShoop.jsp").forward(request, response);
}
展开全部
第一次保存 假如保存在session.put("test",test)中, 然而第二次的时候, 你还是保存在这里面, 所以它会覆盖第一次的结果, 直接把第二次的塞进去,
解决办法:1 把购物车的信息用表保存起来, 每次添加一个商品 就在表中间增加一条数据,
2:购物车保存到session是, session中保存一个集合, 每次在集合中添加一条数据, 在把集合重新保存到session 中,
3:把购物车信息保存在cookie中, 方法和保存在session中,
建议用一个表去保存, 这样方便很多 ,,也省了很多事, 每次用的时候直接查一下数据库, 也便于以后维护
OK! 拿起你的键盘干吧
解决办法:1 把购物车的信息用表保存起来, 每次添加一个商品 就在表中间增加一条数据,
2:购物车保存到session是, session中保存一个集合, 每次在集合中添加一条数据, 在把集合重新保存到session 中,
3:把购物车信息保存在cookie中, 方法和保存在session中,
建议用一个表去保存, 这样方便很多 ,,也省了很多事, 每次用的时候直接查一下数据库, 也便于以后维护
OK! 拿起你的键盘干吧
追问
保存到集合是用List 吗? 我用List list = null;
list.add(item);
指示说list只能为空, 我比较接受你说的第2中办法,但是,应该怎么实现呢
追答
List list =new ArrayList();
list.add(item);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询