求个购物车简单例子 要用JSP+javabean+severlet做的简单点就好了,没做过,不知道什么原理,无从下手 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简单因该创建一个购物车Bean 里面2个属性。。一个是商品。一个是数量然后在Servlet中 把商品放进去。。在New个Map 把商品ID 当Key, 购物车Bean 当值放入Map中放入之前要查询 Map里是否已经有这是商品ID, 如果有则 数量+1 如果没有则放入Map中。最后在jsp页面中显示 Map中的值。。 //大概是这样的: String id = request.getpara....("id"); if (session.getsetAttribute("car") == null) { //用户当前不存在购物车 session.setAttribute(new HashMap()); // 为用户创建一个购物车,map中的key存放商品的唯一标识(ID),value则是商品的javaBean } Map car = (Map)session.getAttribute("car"); if (car.get(id)) { // 如果这个商品已经存在于购物车 //则在商品数量上面+1 } else { // 如果这个商品在购物车中不存在,则查出商品信息,添加到Map } 不是完整代码,还是楼主自己写完代码,上面是流程了原理 发个Servlet LZ看下public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session =request.getSession(false); RequestDispatcher dispatcher; Map cart=(Map)session.getAttribute("cart");//从session中获得购物车 FoodBean food=(FoodBean)session.getAttribute("foodToAdd");//从session中获得要加入购物车的商品 if(cart==null){//判断 购物车是否为空。。如果为空则创建。并放入session cart=new HashMap(); session.setAttribute("cart", cart); } CartItemBean cartItem=(CartItemBean)cart.get(food.getFoodID());//判断map中是否有要添加的商品 ifcartItem!=null){ cartItem.setQuantity(cartItem.getQuantity()+1);//有+1 }else cart.put(food.getFoodID(), new CartItemBean(food,1));//没有则放入。 dispatcher=request.getRequestDispatcher("/ch04/shopCart.jsp");//页面跳转 dispatcher.forward(request, response); } 再发个显示页面部分代码<% Map cart=(Map)session.getAttribute("cart");//取出map if(cart==null||cart.size()==0) out.print("<p>购物车当前为空!</p>"); else{ Set cartItems=cart.keySet(); Object[] foodItems=cartItems.toArray(); double total=0; for(int i=0;i<foodItems.length;i++){// 循环Map中的值(之前要用到Set集合) CartItemBean cartItem=(CartItemBean)cart.get((String)foodItems[i]); FoodBean food=cartItem.getFood(); int quantity=cartItem.getQuantity(); double price=food.getFoodPrice(); double subtotal=quantity*price; total+=subtotal;%> LZ去参考下吧,代码很全的,包括表的结构都存在。http://hi.baidu.com/cjhnihao/blog/item/78dc95ec5d65db2262d09fe4.html javaEE struts2框架 一个老大难问题 点清除button没反应!!! 菜鸟使用Tomcat时的菜鸟问题 有JSP开发小组或兴趣小组的QQ群号没? 做群集时,Hibernate 产生主键的问题? help ji irport高手请进,不是高手也可以歇歇 在JSP中为避免IE前进和后退按钮的问题,请问如何做出以下效果来。 验证放在那? 连数据库为什么总连不上 做后台系统用struts2,页面上用jstl表达式迭代出来多个信息,通过id判断所要删除修改的项,通过button如何传这个id给Action
然后在Servlet中 把商品放进去。。在New个Map 把商品ID 当Key, 购物车Bean 当值放入Map中
放入之前要查询 Map里是否已经有这是商品ID, 如果有则 数量+1 如果没有则放入Map中。
最后在jsp页面中显示 Map中的值。。
//大概是这样的:
String id = request.getpara....("id");
if (session.getsetAttribute("car") == null) { //用户当前不存在购物车
session.setAttribute(new HashMap()); // 为用户创建一个购物车,map中的key存放商品的唯一标识(ID),value则是商品的javaBean
}
Map car = (Map)session.getAttribute("car");
if (car.get(id)) { // 如果这个商品已经存在于购物车
//则在商品数量上面+1
} else {
// 如果这个商品在购物车中不存在,则查出商品信息,添加到Map
}
不是完整代码,还是楼主自己写完代码,上面是流程了原理
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session =request.getSession(false);
RequestDispatcher dispatcher;
Map cart=(Map)session.getAttribute("cart");//从session中获得购物车
FoodBean food=(FoodBean)session.getAttribute("foodToAdd");//从session中获得要加入购物车的商品
if(cart==null){//判断 购物车是否为空。。如果为空则创建。并放入session
cart=new HashMap();
session.setAttribute("cart", cart);
}
CartItemBean cartItem=(CartItemBean)cart.get(food.getFoodID());//判断map中是否有要添加的商品
ifcartItem!=null){
cartItem.setQuantity(cartItem.getQuantity()+1);//有+1
}else
cart.put(food.getFoodID(), new CartItemBean(food,1));//没有则放入。
dispatcher=request.getRequestDispatcher("/ch04/shopCart.jsp");//页面跳转
dispatcher.forward(request, response);
}
<%
Map cart=(Map)session.getAttribute("cart");//取出map
if(cart==null||cart.size()==0)
out.print("<p>购物车当前为空!</p>");
else{
Set cartItems=cart.keySet();
Object[] foodItems=cartItems.toArray();
double total=0;
for(int i=0;i<foodItems.length;i++){// 循环Map中的值(之前要用到Set集合)
CartItemBean cartItem=(CartItemBean)cart.get((String)foodItems[i]);
FoodBean food=cartItem.getFood();
int quantity=cartItem.getQuantity();
double price=food.getFoodPrice();
double subtotal=quantity*price;
total+=subtotal;%>
http://hi.baidu.com/cjhnihao/blog/item/78dc95ec5d65db2262d09fe4.html