문제

I created a hashmap in my SERVLET as follows:

 int productId = Integer.parseInt(request.getParameter("productId"));

 HashMap cartList = new HashMap();
 Cart item = new Cart(productId, productName, price, quantity);
 cartList.put(productId, item);

But I have get the following error:

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'java.util.HashMap$Entry' does not have the property 'productId'.

What does it mean? How can i resolve my error?

EDIT: HERE is my JSP

<c:forEach var="cart" items="${cartList}">
         ${cart.productId}
         ${cart.productName}
    <form method="POST" action="ShoppingCartUpdate">
        <input type="submit" value ="Update" class="loginButton" name="Update">
    </form>
    <form method="POST" action=""ShoppingCartRemove">
        <input type="submit" value ="Remove" class="loginButton" name="Delete">
    </form>
</c:forEach>
도움이 되었습니까?

해결책

When iterating the Map in JSTL you are iterating its Entrys, so that you need to use value property to access their values:

<c:forEach var = "e" items = "${cartList}">
    ${e.value.productId}
</c:forEach>

다른 팁

You need to declare your HashMap as such so that it knows what the type of the key/value pair are. You should always instantiate Hashmaps in this way, and I'm not sure if it will let you even without doing so. I know that in things like Actionscript you can get away with defining a Dictionary and not what types need to be in it, but in Java you must define the types that are being used and you can't use primitive types (I believe) such as int, double etc

HashMap<Integer, Cart> cartList = new HashMap<Integer, Cart>(); 

and the productId must be Integer and not just int

It is due to the way you're trying to read it using a JSP or something.

$cartList[productId] should be done. Note that productId is an integer variable here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top