Results 1 to 2 of 2
Thread: EL & <c:forEach>
- 03-15-2011, 01:06 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
EL & <c:forEach>
Wondering how I can get this done.
I have a class ShoppingCartItem which has a variable called 'Object item' and inside of the ShoppingCart I have a hashmap of ShoppingCartItems called items.
inside my servlet I setup a session attribute called cart which is a shopping cart.
inside my jsp I am trying to do this
<c:forEach var="item" items="${cart.items}">
${item.quantity} // works fine
${item.bookId} // fails
${item.title} // fails...
</c:forEach>
How can I cast the item to a book?
- 03-21-2011, 03:23 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Shopping cart items
Your question is a little unclear. My understanding of your explanation that you have a class called ShoppingCartItem that has in it an instance of Object called item. You are iterating through a list of ShoppingCartItems in your jsp and identifying each instance of ShoppingCartItem as "item". One big guess I have made here is that quantity belongs to the ShoppingCartItem whereas bookId and title belong to the item object instance within ShoppingCartItem.
So it occurs to me that you need to refer to an instance of the Object named "item" within an instance of ShoppingCartItem, which you have called item in your iteration, asYou have inadvertantly made the code more difficult to understand with your choice of names in your jsp and class. Maybe a better choice would be to call your Book instance in ShoppingCartITem something other than item or change your choice of names in your jsp to make it a bit clearer.Java Code:${item.item.bookId}
I don't think you need to worry about casting item as Book. However, I will ask why you can't cast item as Book in the first place?
Similar Threads
-
x:forEach problem
By GrahamButcher in forum XMLReplies: 0Last Post: 10-22-2010, 03:42 PM -
<c:forEach to list data in X columns
By polygrimm in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-04-2009, 08:27 PM -
JSTL forEach Query
By wonderpets in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 07-14-2008, 06:35 PM -
How to use Foreach on an Array
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:06 PM -
Question regarding foreach loop...
By theonlywalks in forum New To JavaReplies: 2Last Post: 03-15-2008, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks