【问题标题】:Hashmap value changed after requesting from session从会话请求后哈希图值更改
【发布时间】:2012-05-08 20:04:59
【问题描述】:

我从事 Java/HTML 项目。我已将哈希图设置为会话属性。我从会话中请求哈希图并将键/值放入其中 map.put("some string", "1") .当第二次执行此操作时,我打印了 hashmap 内容,并且唯一的值,即在 hashmap 上的最后一次操作之后为“1”,变为“-1”。

在我看来,Hashmap 是这个项目最好的数据结构。有人可以帮忙吗?

public class Cart {
    private HashMap<String, asd> list;

    public Cart(){
        list = new HashMap<String, asd>();
    }

    public HashMap<String, asd> getMap(){
        return list;
    }

    /*
     * Parameter: code
     *      -1: increase quantity by 1
     *      0: delete product from the product list
     *      else: set the product quantity to the passed value
     */
    public void alterProduct(int code, String product){
        if(list.containsKey(product)) {
            if(code == -1) plusOne(product);
            if(code == 0) remove(product);
            else setValue(product, code);
        }else {
            asd asd = new asd();
            asd.a = 1;
            list.put(product, asd);
        }
    }

    private void plusOne(String product){
        asd asd = list.get(product);
        asd.a = asd.a + 1;
        list.put(product, asd);
    }

    private void remove(String product){
        list.remove(product);
    }

    private void setValue(String product, int code){
        asd asd = new asd();
        asd.a = code;
        list.put(product, asd);
    }
}

class asd{
    int a;

    public String toString(){
        return ""+a;
    }
}

我将Cart对象设置为会话属性的JSP代码:

<%
    Cart myCart = new Cart();
    session.setAttribute("cart",myCart);
%>

Servlet 代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.alterProduct(-1, (String) request.getSession().getAttribute("name"));
        doGet(request, response);
    }

在我第二次为同一个 (String) request.getSession().getAttribute("name") 调用 alterProduct 后,同一个键的 hashmap 值为'-1'。

【问题讨论】:

    标签: session jakarta-ee hashmap


    【解决方案1】:

    什么是产品的类型/价值?它是如何连接到“购物车”的?

    我猜你弄乱了数据类型是怎么回事。另一种选择是您在 Cart.toString() 方法中有错误。我建议您使用“普通”数据类型更改代码并重新检查。如果失败,请使用您的 Cart 类而不进行混乱的转换并调试代码。

    你这里有错误:

      public void alterProduct(int code, String product){
        if(list.containsKey(product)) {
          if(code == -1) plusOne(product);
          if(code == 0) remove(product);
          else setValue(product, code);
      }
    
    
      private void setValue(String product, int code){
        asd asd = new asd();
        asd.a = code;
        list.put(product, asd);
      }
    

    考虑一下当您第二次拨打art.alterProduct(-1, "something") 时会发生什么。 list.containsKey(product) 为真(您使用相同的产品”),代码为-1。所以

    if(code == -1) plusOne(product); 按预期执行。

    但是你有一些奇怪的东西

    if(code == 0) remove(product); else setValue(product, code);

    code ==0 被评估为假,因此 调用其他指令。你打电话给setValue(product, -1)

    正如您在上面看到的,setValue 会将 -1 分配给您观察到的 asd.a。

    【讨论】:

    • 产品是字符串。 Cart 是初始化 map 的类。我将 HashMap&lt;String, String&gt; 更改为 HashMap&lt;String, MyClass&gt; class MyClass{ int a; } 并且成功了。
    • 你能提供完整的代码吗?您如何创建\检索地图,如何填充它?
    • (facepalm) 对我的问题感到尴尬
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 2012-01-10
    • 2012-06-04
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多