Reputation: 1401
I have ShoppingCart class:
package shoppingcart;
import models.CD;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ShoppingCart {
private List<CD> shoppingCartItemList ;
private long totalPrice ;
public ShoppingCart()
{
shoppingCartItemList = new ArrayList<CD>() ;
totalPrice = 0 ;
}
public void addCdToCart(CD cd)
{
shoppingCartItemList.add(cd) ;
totalPrice += cd.getPrice();
}
public List<CD> getShoppingCartItems()
{
return this.shoppingCartItemList ;
}
public long getTotalPrice()
{
return totalPrice;
}
}
I use this class in a servlet:
ShoppingCart shoppingCart = new ShoppingCart();
CD cd = new CD(1, "DAVID", 10, 1);
shoppingCart.addCdToCart(cd);
request.getSession().setAttribute("shoppingCart", shoppingCart);
request.setAttribute("servletMessage", "CD added to Cart");
As you can see, I have a list and i have to iterate this in my jsp. I import the ShoppingCard class in my jsp:
<%@ page import="shoppingcart.ShoppingCart"%>
<c:forEach items="${shoppingCart.getShoppingCartItems}" var="cart">
<form method="POST" action="${pageContext.request.contextPath}/removeFromCart">
<input type="hidden" name="cdId" value="${cart.title}" />
<tr>
<td><c:out value="${cart.title}" /></td>
<td align="center"><c:out value="${cart.category}" /></td>
<td align="center"><c:out value="${cart.price}" /></td>
<td align="center" width="25%"><input type="submit" value="Remove" name="action" style="height:30px; width: 70px;font-size:10pt;"></td>
</tr>
</form>
</c:forEach>
I get this error:
org.apache.jasper.el.JspPropertyNotFoundException: /myCart.jsp(85,4) '${shoppingCart.getShoppingCartItems}' Property 'getShoppingCartItems' not found on type shoppingcart.ShoppingCart. What can I solve this problem?
Should I change something?
Edited because of the extra added code.
Upvotes: 0
Views: 5555
Reputation: 691973
The shopping cart is stored in an attribute named "shoppingCart". So it should be something like
<c:forEach items="${shoppingCart.xxx}" ...
where xxx is the property allowing to access the list inside the ShoppinCart instance. But you don't have such a property. So, add the following method the ShoppingCart:
public List<CD> getElements() {
return this.shoppingCartItemList;
}
and use
<c:forEach items="${shoppingCart.elements}" ...
Note that naming "shoppingCartItemList" an attribute of the class "ShoppinCart" is redundant and verbose. That's why I chose to name the getter getElements()
: shoppingCart.elements
is much easier to read and natural than shoppingCart.shoppingCartItemList
. You should rename the private field to elements
as well.
Upvotes: 1
Reputation: 71
I recommend you to use JSTL for iterating objects in JSP
here is a case similar to this: JSTL iterate over list of objects
here is an example: http://www.journaldev.com/2090/jstl-tutorial-with-examples-jstl-core-tags
here is a jstl tutorial: http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
it has more things to have a better source code. I hope this can help you...
Upvotes: 0