Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
| Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 22nd, 2006, 06:04 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning JSP question about JSTL

I kinda got bogged down to this particular example program in Beginning JavaServer Pages book. I can’t seem to make sense of this particular code segments. This is found in page 203. Just added the line no. for reference.


1. <jsp:useBean id="lineitems" class="java.util.ArrayList" scope="session"/>
2.
3. <c:choose>
4. <c:when test="${param.action == 'clear'}">
5. ${wxshop:clearList(lineitems)}
6. </c:when>
7.
8. <c:when test="${param.action == 'inc' || param.action=='buy'}">
9. <c:set var="found" value="false"/>
10.
11. <c:forEach var="curItem" items="${lineitems}">
12.
13. <c:if test="${(curItem.sku) == (prod.sku)}">
14. <jsp:setProperty name="curItem"
                      property="quantity"
15. value="${curItem.quantity + 1}"/>
16. <c:set var="found" value="true" />
17. </c:if>


On line no. 1, “lineitems” is of type ArrayList. Then we see on line no. 11, “curItem” takes on the object “lineitems” which is clearly of ArrayList type. Then to the heart of the problem: (on line no. 14) how in the world does “curItem” have access to the “quantity” property of LineItem.java when “lineitems” is declared as type “ArrayList” in line no. 1?

A lot of good things have been said on JSTL/EL, but, men, I think scriptlets are far better to understand than cryptic JSTL/EL.

I hope somebody can shed light to my problem. As you might have guessed, I’m a Java/JSP newbie. Thanks in advance
  #2 (permalink)  
Old July 1st, 2006, 07:46 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<c:forEach> iterates over each item in the "items" parameter, and puts the item itself into the variable defined in "var". Therefore, while inside <c:forEach>, in your example "curItem" equals an element inside the lineitems array. So when the jsp:setProperty is run on the curItem variable, it's affecting one of the items inside lineitems, not lineitems itself.

In Java, the above code would be something like this:

Iterator i = lineitems.iterator();
while (i.hasNext())
{
  Object curItem = i.next();

  /* As you can see, in here curItem is an element of
     lineitems. Note that jsp:setProperty uses reflection
     to run the getter and setter methods on curItem. */
}

Hope this helps!

Jon Emerson
http://www.jonemerson.net/
  #3 (permalink)  
Old July 6th, 2006, 03:32 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your answer. Appreciate your time



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wroc Beginning JSP 2.0 wireblock All Other Wrox Books 11 November 7th, 2004 08:43 PM
Beginning JSP 2.0 chapter 2 Charles Howard JSP Basics 2 May 17th, 2004 12:32 AM
Beginning JSP colin2455 Forum and Wrox.com Feedback 2 March 30th, 2004 03:52 PM
Beginning JSP 2.0 + JSTL AlanZ3 JSP Basics 2 September 28th, 2003 06:58 AM
"Advanced" example in beginning JSP Darren JSP Basics 1 July 23rd, 2003 10:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.