Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

JSTL forEach

Thread view: 
U.O - 26 Mar 2007 22:01 GMT
I can't understand how <forEach> is supposed to work. This code iterates
once (why?) and produces only one string, "Item" (without the index number).
Why?

<jsp:root version="1.2"
   xmlns:jsp="http://java.sun.com/JSP/Page"
   xmlns:c="http://java.sun.com/jsp/jstl/core"
 >
 <jsp:output doctype-root-element="html" omit-xml-declaration="false"
   doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"

doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
 <jsp:directive.page contentType="text/html; charset=UTF-8" session="true"
/>
 <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
     <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1" />
     <title>Blah</title>
   </head>
   <body>
     <div>
       <c:forEach var="i" start="1" end="10">
          Item ${i}<br />
       </c:forEach>
     </div>
   </body>
 </html>
</jsp:root>
david.karr - 27 Mar 2007 00:05 GMT
> I can't understand how <forEach> is supposed to work. This code iterates
> once (why?) and produces only one string, "Item" (without the index number).
[quoted text clipped - 25 lines]
>   </html>
> </jsp:root>

I can't say for sure, as I've never tried to use pure XML JSP pages,
but it sure seems like it's not executing the JSTL at all.  Is the
fact that you have version "1.2" on your JSP a problem?
U.O - 27 Mar 2007 07:21 GMT
> > I can't understand how <forEach> is supposed to work. This code iterates
> > once (why?) and produces only one string, "Item" (without the index number).
[quoted text clipped - 6 lines]
> >   <jsp:output doctype-root-element="html" omit-xml-declaration="false"
> >     doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"

doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
> >   <jsp:directive.page contentType="text/html; charset=UTF-8" session="true"
> > />
[quoted text clipped - 17 lines]
> but it sure seems like it's not executing the JSTL at all.  Is the
> fact that you have version "1.2" on your JSP a problem?

Right, the <c:forEach> tag is still in the generated output. The funny thing
is, when I add my own taglib, my tags are processed correctly.

Generated xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type"/>
   <title>Blah</title>
 </head>
 <body>
   <div>
     <c:forEach end="10" start="1" var="i">
       Item <br/>
     </c:forEach>
   </div>
 </body>
</html>
steen - 27 Mar 2007 12:16 GMT
>       <c:forEach end="10" start="1" var="i">
>         Item <br/>
>       </c:forEach>

Looks to me like you're forgetting to add a reference to the taglib:
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>

because when you forget this, the <c:forEach> tag will be sent to the
client (like your output states)

/Steen
festo - 27 Mar 2007 19:18 GMT
> >       <c:forEach end="10" start="1" var="i">
> >         Item <br/>
[quoted text clipped - 7 lines]
>
> /Steen

Did you try to use <c:out value="Item ${i}" /> ?

papaJo
U.O - 27 Mar 2007 19:33 GMT
> > >       <c:forEach end="10" start="1" var="i">
> > >         Item <br/>
[quoted text clipped - 9 lines]
>
> Did you try to use <c:out value="Item ${i}" /> ?

Yes. Same result.

> papaJo
U.O - 27 Mar 2007 19:30 GMT
> >       <c:forEach end="10" start="1" var="i">
> >         Item <br/>
> >       </c:forEach>
>
> Looks to me like you're forgetting to add a reference to the taglib:
> <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>

Doesn't
xmlns:c="http://java.sun.com/jsp/jstl/core"
do that?

> because when you forget this, the <c:forEach> tag will be sent to the
> client (like your output states)
>
> /Steen
Daniel Pitts - 27 Mar 2007 00:24 GMT
> I can't understand how <forEach> is supposed to work. This code iterates
> once (why?) and produces only one string, "Item" (without the index number).
[quoted text clipped - 25 lines]
>   </html>
> </jsp:root>

Have you considered using a modern version of JSPs? Say, JSP 2?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.