• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting JSTL forEach to work

 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, all!
I am starting to play around with the JSTL but have run into a problem on my first attempt. I can't figure out if I am setting a var improperly or trying to display the wrong thing.
Code snippets from my JSP are below. This is not the whole thing and depends on a CSS anyway.

What I want to do is display a table with 5 rows and each row contains one string from the msgs array. What I get is a table with a single row that displays the string $msg in it. So it would seem that msgs is not being treated as an array or that I am accessing it incorrectly (do I need to do any casting?).
Suggestions will be appreciated!
Thanks.
 
Author
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Wetherbie:
<c:forEach var="msg" items="$page:msgs">
<tr bgcolor="#FFFFFF">
<td class="<%= rowType %>" height="19"><c:out value="$msg"/></td>
</tr>
</c:forEach>
</table>


Hi John. You're close, but the syntax you're using has been out of date for quite some time; it belongs to a very preliminary "early access" draft of the specification. (Some books oddly covered this syntax even though the draft was widely publicized as being preliminary and prone to change.)
Instead of "$page:msgs", you want to say "${pageScope.msgs}". Instead of "$msg", you want "${msg}". JSTL expressions begin with "${" and end with "}".
Also, the way you're using <c:set> in your example
is questionable. It sets the variable "msgs" to the literal string "msgs", which is probably not what you want. You probably mean to add the line
pageContext.setAttribute("msgs", msgs);
in your scriptlet block. For a variety of reasons, <c:set> is not a good conduit between scripting variables and scoped variables (called "scoped attributes" in older JSP specifications).
Please let me know if you have any other questions. The JSTL spec is available from http://java.sun.com/products/jstl, and my book ("JSTL in Action") will be out shortly.
[ July 29, 2002: Message edited by: Shawn Bayern ]
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shawn,
Thanks! Implemented your suggestions and now it works. One interesting(?) thing I noticed was that when I commented out the c:set using <-- --> it did not work. I needed to delete the c:set from the page or use a Java comment (//) right before the c:set for things to work correctly.
BTW, the syntax I was using is from Sun's Web Services tutorial. I looked at the spec to fix some of the problems I had but I wish there were some exmples in it!
Are there are current sources of examples with the correct syntax? How soon will your book be out?
Thanks again,
[ July 30, 2002: Message edited by: John Wetherbie ]
 
Shawn Bayern
Author
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Wetherbie:

Thanks! Implemented your suggestions and now it works. One interesting(?) thing I noticed was that when I commented out the c:set using <-- --> it did not work. I needed to delete the c:set from the page or use a Java comment (//) right before the c:set for things to work correctly.


Right. HTML comments are treated just like other text from the perspective of a JSP container. That is, it sees "<!--" and sends it to the browser; it doesn't stop processing the page. Comments like this are meant to be ignored by browsers, not JSP pages. To comment out part of a JSP page, you can use syntax like "<%--" and "--%>".



BTW, the syntax I was using is from Sun's Web Services tutorial. I looked at the spec to fix some of the problems I had but I wish there were some exmples in it!


Well, the spec is primarily for developers and designers of JSTL implementations. For examples, books are probably best -- though of course I have a vested interest in saying so.



Are there are current sources of examples with the correct syntax? How soon will your book be out?


My book ships out (to bookstores) early next week. Lots of people are preordering it from Amazon; preorders will probably ship in mid-August. You can also get an ebook today from http://www.manning.com/bayern . (Manning effectively refunds the purchase price of an ebook when you buy the printed book from them.)
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic