- Struts 购物车的实现 用Map集合
- 首先登陆后就给用户一台的购物车
- login.action:
- String uname=loginForm.getUname();
- String upass=loginForm.getUpassword();
- boolean flag=InvokeUser.getLoginResult(uname, upass);
- if(flag)
- {
- ArrayList list=GoodM.getGoodsList();
- request.getSession().setAttribute("goodslist", list);
- ShoppingCart cart=ShoppingCart.getInstance();
- request.getSession().setAttribute("cart", cart);
- return mapping.findForward("success");
- }
- return mapping.getInputForward();
- 登陆成功后跳到产品页面
- show.jsp:
- <table border="1">
- <logic:present name="goodslist">
- <logic:iterate id="li" name="goodslist" scope="session">
- <logic:present name="li">
- <tr>
- <td><bean:write name="li" property="gid"/></td>
- <td><bean:write name="li" property="gname"/></td>
- <td><bean:write name="li" property="price"/></td>
- <form action="buy.do" method="get" name="form1">
- <td><input type="hidden" name="gid" value="${li.gid}"/></td>
- <td><input type="text" name="number"/></td>
- <td><input type="submit" value="submit"/></td>
- </form>
- </tr>
- </logic:present>
- </logic:iterate>
- </logic:present>
- </table>
- 选择购买的物品后到BuyAction:
- String gid=request.getParameter("gid");
- int count=Integer.parseInt(request.getParameter("number"));
- Goods goods=GoodM.findGoodsById(gid);
- OrderItems order=null;
- float sumMoney=0;
- HttpSession session=request.getSession();
- Map<String,OrderItems> map=null;
- ShoppingCart cart=(ShoppingCart)session.getAttribute("cart");
- order=new OrderItems();
- order.setOrdercount(count);
- order.setGood(goods);
- order.setOrdersum();
- cart.addCart(gid, order,count);
- session.setAttribute("getGoods", cart.getCartAll());
- sumMoney=cart.getSumCart();
- session.setAttribute("sumMoney", sumMoney);
- return mapping.findForward("checkout");
- 添加到购物车后到checkout.jsp(察看自己买的产品和总价等信息)
- <table border="1">
- <logic:present name="getGoods">
- <logic:iterate id="li" name="getGoods" scope="session">
- <logic:present name="li">
- <tr>
- <td><bean:write name="li" property="key"/></td>
- <td><bean:write name="li" property="value.good.gname"/></td>
- <td><bean:write name="li" property="value.good.price"/></td>
- <td><bean:write name="li" property="value.ordercount"/></td>
- <td><bean:write name="li" property="value.ordersum"/></td>
- </tr>
- </logic:present>
- </logic:iterate>
- </logic:present>
- </table>
- 你一共花了:${sumMoney}
- <html:link page="/show.jsp">continue buy</html:link><br><br>
- <html:link page="/go.jsp">checkout</html:link>
- 如果还购买那就继续买否则结帐到go.jsp(清理购物车并退出)
- <%
- ShoppingCart cart=(ShoppingCart)session.getAttribute("cart");
- cart.removeAllCart();
- session.removeAttribute("cart");
- session.removeAttribute("getGoods");
- session.removeAttribute("sumMoney");
- %>
- 结帐成功!<br><br>
- <a href="login.jsp">login in</a>
- 最关键的购物车代码:
- public class ShoppingCart {
- private Map<String,OrderItems> map = new HashMap<String,OrderItems>();
- private static ShoppingCart cart = null;
- private ShoppingCart() {
- }
- public static ShoppingCart getInstance() {
- if (cart == null) {
- cart = new ShoppingCart();
- }
- return cart;
- }
- public void addCart(String id, OrderItems order,int count) {
- if (map.containsKey(id)) {
- OrderItems or = (OrderItems) map.get(id);
- map.remove(or); //如果有存在的先移除后在重新添加
- or.setOrdercount(or.getOrdercount() + count);
- or.setOrdersum();
- map.put(id, or);
- } else {
- map.put(id, order);
- }
- }
- public void removeCart(Integer id){
- if(map.containsKey(id)){
- map.remove(id);
- }
- }
- public void removeAllCart(){
- if(!map.isEmpty()){
- map.clear();
- }
- }
- public float getSumCart()
- {
- float sum=0;
- Iterator<OrderItems> iter = this.map.values().iterator();
- while (iter.hasNext()) {
- OrderItems element = (OrderItems) iter.next();
- sum+=element.getOrdersum();
- }
- return sum;
- }
- public Map getCartAll() {
- return this.map;
- }
- }
- DAo层:
- public class DBManager extends Object{
- private Connection con = null;
- private Statement stmt = null;
- private ResultSet rs = null;
- public void init()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- //加载驱动程序
- con = DriverManager.getConnection("jdbc:mysql://192.168.1.11/ttt?
- user=root&password=0&useUnicode=true&characterEncoding=gbk");
- //创建连接
- }catch(Exception e){}
- }
- //获取连接
- public Connection getConnection()
- {
- if(con == null)
- init();
- return con;
- }
- //执行有结果集的查询
- public int executeUpdate(String sql) throws SQLException
- {
- stmt = getConnection().createStatement();
- return stmt.executeUpdate(sql);
- }
- public ResultSet executeQuery(String sql) throws SQLException
- {
- stmt = getConnection().createStatement();
- return stmt.executeQuery(sql);
- }
- public void close()
- {
- if(rs!=null)
- try {
- rs.close();
- } catch (SQLException e) { }
- if(stmt!=null)
- try {
- stmt.close();
- } catch (SQLException e) { }
- if(con!=null)
- try {
- con.close();
- } catch (SQLException e) { }
- }
- }
- 附:
- GoodM.java:
- public class GoodM {
- public static ArrayList getGoodsList()
- {
- DBManager dbm = new DBManager();
- ArrayList list = new ArrayList();
- try {
- ResultSet rs = dbm.executeQuery("select * from goods");
- while(rs.next())
- {
- Goods goods = new Goods();
- goods.setGid(rs.getString(1));
- goods.setGname(rs.getString(2));
- goods.setPrice(rs.getFloat(3));
- //goods.setQuantity(rs.getInt(4));
- list.add(goods);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- dbm.close();
- return list;
- }
- public static Goods findGoodsById(String id)
- {
- DBManager dbm = new DBManager();
- Goods goods = new Goods();
- try {
- StringBuffer sql = new StringBuffer();
- sql.append("select * from goods where gid='");
- sql.append(id);
- sql.append("'");
- ResultSet rs = dbm.executeQuery(sql.toString());
- while(rs.next())
- {
- goods.setGid(rs.getString(1));
- goods.setGname(rs.getString(2));
- goods.setPrice(rs.getFloat(3));
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- dbm.close();
- return goods;
- }
- }
- 基础类:
- Goods:
- public class Goods {
- private String gid;
- private String gname;
- private float price;
- public String getGid() {
- return gid;
- }
- public void setGid(String gid) {
- this.gid = gid;
- }
- public String getGname() {
- return gname;
- }
- public void setGname(String gname) {
- this.gname = gname;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- }
- OrderItems订单类:
- public class OrderItems {
- private int oid;
- private float ordersum;
- private int ordercount;
- private Goods good;
- public Goods getGood() {
- return good;
- }
- public void setGood(Goods good) {
- this.good = good;
- }
- public int getOid() {
- return oid;
- }
- public void setOid(int oid) {
- this.oid = oid;
- }
- public int getOrdercount() {
- return ordercount;
- }
- public void setOrdercount(int ordercount) {
- this.ordercount = ordercount;
- }
- public float getOrdersum() {
- return ordersum;
- }
- public void setOrdersum() {
- this.ordersum = ordercount * good.getPrice();
- }
- }
Struts 购物车的实现 用Map集合
最新推荐文章于 2021-11-29 21:22:55 发布