哈希映射错误:javax.el.PropertyNotFoundException
我在 SERVLET 中创建了一个哈希图,如下所示:
int productId = Integer.parseInt(request.getParameter("productId"));
HashMap cartList = new HashMap();
Cart item = new Cart(productId, productName, price, quantity);
cartList.put(productId, item);
但出现以下错误:
org.apache.jasper.JasperException:javax.el.PropertyNotFoundException:类“java.util.HashMap$Entry”没有属性“productId”。
这是什么意思?我该如何解决我的错误?
编辑:这是我的 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 JSTL 中迭代
Map
时,您正在迭代其Entry
,因此您需要使用value
属性来访问它们的值:When iterating the
Map
in JSTL you are iterating itsEntry
s, so that you need to usevalue
property to access their values:您需要这样声明 HashMap,以便它知道键/值对的类型。你应该总是以这种方式实例化哈希图,我不确定即使不这样做它是否会让你这么做。我知道在像 Actionscript 这样的东西中,您可以定义一个字典,而不是其中需要什么类型,但在 Java 中,您必须定义正在使用的类型,并且不能使用原始类型(我相信)这样如 int、double 等
,并且 ProductId 必须是 Integer 而不仅仅是 int
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
and the productId must be Integer and not just int
这是由于您尝试使用 JSP 或其他方式读取它的方式所致。
$cartList[productId]
应该完成。请注意,productId
在这里是一个整型变量。It is due to the way you're trying to read it using a JSP or something.
$cartList[productId]
should be done. Note thatproductId
is an integer variable here.