当前位置: 代码迷 >> J2EE >> ajax 验证是否已经添加过商品有关问题
  详细解决方案

ajax 验证是否已经添加过商品有关问题

热度:302   发布时间:2016-04-22 02:28:16.0
ajax 验证是否已经添加过商品问题
JScript code
function addProduct(img,productId) {                $("#buy").css("display","none");                $("#cartInfo_"+productId).html('<img align="bottom" src="../images/load.gif" width="14" height="14" style="display:inline" align="bottom"/>&nbsp;购买中...');                $.post("../cart/cart!add.action",                        { id: productId, time: (new Date()).getTime() },                         function(json){                              if(json.ok) {                              alert(json.ok);                                  //没有添加成功                                  $("#buy").css("display","block");                                  $("#cartInfo_"+productId).html('<img align="bottom" src="../images/wrong.gif" width="14" height="14" style="display:inline" align="bottom"/>&nbsp;<span style="color:red">购买失败</span>');                              }                              else {                                  //添加成功                                  $("#cartInfo_"+productId).html('<img align="bottom" src="../images/right.gif" width="14" height="14" style="display:inline" align="bottom"/>&nbsp;购买成功');                                  var timeId=setTimeout(function(){                                      clearTimeout(timeId);                                      $("#buy").css("display","block");                                      $("#cartInfo_"+productId).html("");                                  }, 2000);                              }                        },"json"                    );             }


Java code
    public String add() {        System.out.println("id:"+id);        CartDAO dao=DAOFactory.getCartDAO();        //String flag=DAOFactory.getCartDAO().add(id);//返回yes or no        String flag=dao.add(id);        if(flag.equals("yes")) { //如果存在,返回OK            ok=true;        } else {            ok=false;        }        return "success";    }


Java code
    public Map<Integer, CartItem> store = new HashMap<Integer, CartItem>();    public String add(int id) {        if (store.containsKey(id)) { // 是否已经添加购买            return "yes";        } else {            Product pro;            try {                pro = DAOFactory.getProductDAO(Constant.TYPE_BOOK).findByProId(                        id);                CartItem cart = new CartItem();                cart.setNum(1);                cart.setProduct(pro);                cart.setBuy(true);                store.put(id, cart);                return "no";            } catch (Exception e) {                e.printStackTrace();            }        }        return null;    }

json能通过,但是每次都是购买成功,我想知道这是为什么,我用main测试这个上面这个方法,添加两个重复的ID是返回yes的,但是网页上就不行,点一次再点一次还是购买成功,我想知道是不是和HashMap生命周期,请大家帮帮忙,看看哪里不对。上面这个方法在struts2里的一个action

------解决方案--------------------
你用的struts2 吧,struts2不是单例的,你每次请求都new一个action,所以store都没数据。
------解决方案--------------------
可以把store放到session里。
  相关解决方案