wood burning stoves*
The moose likes JSP and the fly likes Getting JSTL forEach to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting JSTL forEach to work" Watch "Getting JSTL forEach to work" New topic
Author

Getting JSTL forEach to work

John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
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.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by John Wetherbie:
<c:forEach var="msg" items="$page:msgs">
<tr bgcolor="#FFFFFF">
<td class="<%= rowType %>" height="19"><cut 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 ]

Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
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
Ranch Hand

Joined: May 06, 2002
Posts: 160
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting JSTL forEach to work
 
Similar Threads
Table data display with JSTL in a JSP
JSTL when test problem
Pagination in jsp
pass jstl sql query result to jsp
JSTL and Results