开发者

Hashmap Error: javax.el.PropertyNotFoundException

I created a hashmap in my SERVLET as follows:

 int pro开发者_运维知识库ductId = 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜