我用SSH做项目,用分发ACTION跳转页面,成功给查询MYSQL中的数据,却不知道怎样把查到的数据加入购物车,求各位老师给予指点

解决方案 »

  1.   

    session里面存个map,key是商品的id,value是数量。
    shopping_car.jsp<%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="java.util.*"%>
    <%
    request.setCharacterEncoding("GBK");
    %>
    <%
    int pid=0;
    int pnum=0;
    try
    {
    pid=Integer.parseInt(request.getParameter("pid"));
    }
    catch(Exception ex)
    {
    }
    Map<Integer,Integer> mp=(Map<Integer,Integer>)session.getAttribute("shoppingCar");
    if(mp==null)
    {
    mp=new HashMap<Integer,Integer>();
    }
    if(mp.get(pid)==null)
    {
    mp.put(pid,1);
    }
    else
    {
    pnum=mp.get(pid);
    pnum++;
    mp.put(pid,pnum);
    }
    session.setAttribute("shoppingCar",mp);
    %>
    <jsp:forward page="shopping_car_list.jsp"/>
    shopping_car_list.jsp<%@ page contentType="text/html" pageEncoding="GBK"%>
    <%@ page import="org.xiong.demo.vo.*,org.xiong.demo.factory.*,java.util.*"%>
    <%-- <%@ page import="java.sql.*,java.util.*"%> --%>
    <%
    request.setCharacterEncoding("GBK");
    %>
    <script language="javascript">
    function ProductCheck()
    {
    window.location="product_check.jsp";
    }
    </script>
    <%
    Map mp=(Map)session.getAttribute("shoppingCar");
    List<Product> allProduct=null;
    if(mp!=null)
    {
    Set kst=mp.keySet();
    allProduct=DaoFactory.getProductDaoInstance().findShoppingCar(kst);%>
    <center>
    <h1>已添加至购物车中的商品</h1>
    </center>
    <form action="update_product.jsp" method="post">
    <table border="1" width="100%">
    <tr>
    <th>商品编号</th>
    <th>商品名称</th>
    <th>简介</th>
    <th>价格</th>
    <th>购买数量</th>
    <th>累计点击率</th>
    </tr>
    <%
    Iterator<Product> iterPro=allProduct.iterator();
    while(iterPro.hasNext())
    {
    Product pr=iterPro.next();
    int pid=pr.getPid();
    String name=pr.getName();
    String note=pr.getNote();
    float price=pr.getPrice();
    int amount=pr.getAmount();
    int cnt=pr.getCount();
    %>
    <tr>
    <th><%=pid%></th>
    <th><%=name%></th>
    <th><%=note%></th>
    <th><%=price%></th>
    <th><input name="<%=pid%>" value="<%=mp.get(pid)%>" size="3" maxlength="3"/></th>
    <th><%=cnt%></th>
    </tr>
    <%
     }

    %>
    <tr >
    <th colspan="6"><input type="submit" value="保存修改" />&nbsp;&nbsp;&nbsp;<input type="button" value="生成订单" onClick="ProductCheck()"/></th>

    </tr>
    </table>
    </form>
    <%

    }
    else
    {
    %>
    <table border="1" width="100%">
    <tr>
    <th>购物车中没有添加商品!</th>
    </tr>
    </table>
    <%
    }

    %>
      

  2.   

    购物车的数据量还是比较大的,而且每个用户都可能会有,放在Session里,合适吗?
      

  3.   

    <%@ page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*,java.util.*,org.gjt.mm.mysql.*"%>
    <html>
    <%
        request.setCharacterEncoding("UTF-8");
    %>
    <%
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/shoppingchart?useUnicode=true&characterEncoding=UTF-8", "root","123");
    Statement stmt=conn.createStatement();
    ResultSet rest=stmt.executeQuery("select * from shoppingchart where userId=12");
    %>
    <h1>已添加至购物车中的商品</h1>    
    <form action="update_product.jsp" method="post">
    <table border="1" width="100%">
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>简介</th>
                <th>价格</th>
                <th>购买数量</th>    
            </tr>
    <%        
                while(rest.next())
                {
                    int productId=rest.getInt(1);
                    String name=rest.getString(3);
                    String note=rest.getString(4);
                    String price=rest.getString(5);
                    int amount=rest.getInt(6);
    %>
            <tr>
                    <th><%=productId%></th>
                    <th><%=name%></th>
                    <th><%=note%></th>
                    <th><%=price%></th>
                    <th><%=amount%></th>                
            </tr>    
    <%
             }            
            
    %>
    </table>
    </form>
    <%        
        }catch(Exception e){
    %>
    <%=e %>
    <% 
        }
    %>
    </html>
      

  4.   

    <%@ page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*,java.util.*,org.gjt.mm.mysql.*"%>
    <html>
    <%
        request.setCharacterEncoding("UTF-8");
    %>
    <%
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/shoppingchart?useUnicode=true&characterEncoding=UTF-8", "root","123");
    Statement stmt=conn.createStatement();
    ResultSet rest=stmt.executeQuery("select * from shoppingchart where userId=12");
    %>
    <h1>已添加至购物车中的商品</h1>    
    <form action="update_product.jsp" method="post">
    <table border="1" width="100%">
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>简介</th>
                <th>价格</th>
                <th>购买数量</th>    
            </tr>
    <%        
                while(rest.next())
                {
                    int productId=rest.getInt(1);
                    String name=rest.getString(3);
                    String note=rest.getString(4);
                    String price=rest.getString(5);
                    int amount=rest.getInt(6);
    %>
            <tr>
                    <th><%=productId%></th>
                    <th><%=name%></th>
                    <th><%=note%></th>
                    <th><%=price%></th>
                    <th><%=amount%></th>                
            </tr>    
    <%
             }            
            
    %>
    </table>
    </form>
    <%        
        }catch(Exception e){
    %>
    <%=e %>
    <% 
        }
    %>
    </html>
      

  5.   

    <%@ page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*,java.util.*"%>
    <html>
    <%
        request.setCharacterEncoding("UTF-8");
    %>
    <%
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/shoppingchart?useUnicode=true&characterEncoding=UTF-8", "root","123");
    Statement stmt=conn.createStatement();
    ResultSet rest=stmt.executeQuery("select * from shoppingchart where userId=12");
    %>
    <h1>已添加至购物车中的商品</h1>    
    <form action="update_product.jsp" method="post">
    <table border="1" width="100%">
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>简介</th>
                <th>价格</th>
                <th>购买数量</th>    
            </tr>
    <%        
                while(rest.next())
                {
                    int productId=rest.getInt(1);
                    String name=rest.getString(3);
                    String note=rest.getString(4);
                    String price=rest.getString(5);
                    int amount=rest.getInt(6);
    %>
            <tr>
                    <th><%=productId%></th>
                    <th><%=name%></th>
                    <th><%=note%></th>
                    <th><%=price%></th>
                    <th><%=amount%></th>                
            </tr>    
    <%
             }            
            
    %>
    </table>
    </form>
    <%        
        }catch(Exception e){
    %>
    <%=e %>
    <% 
        }
    %>
    </html>
    刚发的排版有点乱我这个是从数据库中查出来的,但是感觉还是应该存在客户端cookie中比较好