当前位置: 代码迷 >> J2EE >> 急Struts实现购物车,该如何处理
  详细解决方案

急Struts实现购物车,该如何处理

热度:392   发布时间:2016-04-22 03:29:52.0
急急!!!!Struts实现购物车
用Struts做一个电子商务网站,不知道如何实现购物车,请各位大虾帮帮忙!!!
我的想法是在首页中实现将商品添加到购物车中,请问可以用什么方法实现?

------解决方案--------------------

------解决方案--------------------
1.session
2.建一个购物车的表

------解决方案--------------------
Java code
<%    String user_name = (String) session.getAttribute("user_name");    if (user_name == null)     {        out.println("<script>");        out.println("window.alert('你没有登陆,请先登录!');");        out.println("window.location=('user_login.jsp');");        out.println("</script>");    }    //得到购物车信息    ArrayList orders_list = (ArrayList) session.getAttribute("orders_list");    if (orders_list == null || orders_list.size() == 0) {        out.println("<script>");        out.println("window.alert('你的购物车为空');");        out.println("window.location=('product_group.jsp');");        out.println("</script>");    } else {        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");        String dateTime = sdfDateTime.format(new Date());        out.print(dateTime);                  //得到当时时间信息        String order_name = user_name;        //订单名称        order_name += ":";        order_name += dateTime;                String product_names = "";                   String order_counts = "";        String order_moneys = "";        int count = 0;                       //记录购买多少种商品        double order_money = 0;        double total_money = 0;        String order_datetime = dateTime;        String list_datetime = dateTime;        String result2 = null;        String result3 = null;        Iterator it = orders_list.iterator();        while (it.hasNext())         {            OrderList shop = (OrderList) it.next();            if (user_name.equals(shop.getUserName()))             {                product_names += shop.getProductName() + ":";                order_counts += shop.getOrderCount() + ":";                count++;                                String product_name = shop.getProductName();                int order_count = shop.getOrderCount();                String list_name = user_name;                list_name += ":";                list_name += product_name;                                double product_price = 0;                String image_name = "";                String sql1 = "select product_price,image_name from products,images "                        + "where products.product_name like '%" + product_name.trim()                        + "%' and products.product_name = images.product_name";                database.setType(2);             //访问数据库,提取商品相关的信息                database.setKeyword(sql1);                StringBuffer b1 = database.byKeywordInquire();                String result1 = b1.toString();                StringTokenizer stkInfo = new StringTokenizer(result1, "#");                stkInfo.nextElement();                product_price = Double.parseDouble((String) stkInfo.nextElement());                image_name = (String) stkInfo.nextElement();                order_money = product_price*order_count;                order_money = ((int)(order_money*100))/100;                                   order_moneys += String.valueOf(order_money) + ":";                total_money = total_money + order_money;                                String sql2 = "insert into orderslist (list_name, user_name, product_name,product_price,"                            +"order_count,order_money,image_name,order_name,list_datetime)"                    + " values('"                    + list_name                    + "','"                    + user_name                    + "','"                    + product_name                    + "',"                    + product_price                    + ","                    + order_count                    + ","                    + order_money                    + ",'"                    + image_name                     + "','"                     + order_name                    + "','"                     + list_datetime +"')";                database.setType(1); //访问数据库                database.setKeyword(sql2);                StringBuffer b2 = database.byKeywordInquire();                result2 = b2.toString();            }        }                    String sql3 = "insert into orders (order_name, user_name, product_names, total_money, order_datetime)"        + " values('" + order_name + "','" + user_name + "','" + product_names + "'," + total_money +",'" + order_datetime        + "')";        database.setType(1); //访问数据库        database.setKeyword(sql3);        StringBuffer b3 = database.byKeywordInquire();        result3 = b3.toString();                if(result2.equals("success") && result3.equals("success"))        {            out.println("<script>");            out.println("window.alert('购物成功!感谢你购买我们的商品!');");            out.println("</script>");        }                session.removeAttribute("orders_list");        String mailContent = null;        mailContent = "<font color=blue><h2>感谢你购买我们的商品!</h2></font><br>"            + "订单名称: " + order_name + "<br>"            + "订单用户: " + user_name + "<br>"            + "订单商品详细信息: <br><hr>";         mailContent += "<table width=400><tr>";        mailContent += "<td width='20%'>商品名</td>";        mailContent += "<td width='20%'>购买量</td>";        mailContent += "<td width='40%'>单个商品总价</td></tr>";%>
  相关解决方案