Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

ForEach: how to get array data ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an array, I want to fill all data into two columns in a table. If we just use scriplet, it will be simple. but how to do it in JSTL.


in the JSTL, the tricky thing is , how to get array[i], array[i+1] in the ForEach ?
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This test page:



should give you enough clues to figure out your answer on your own.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I almost got there using your way. Now only problem left is , how to
<c:if test="${loop.index+1 < xyz.length }"> , the "xyz.length" has error message . I try to set variable, still not work.

Need your help.

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per forum instructions, you should have included what version of JSP and JSTL you are using.

If using JSP 2.0 and JSTL 1.1, you can use the fn:length() function to determine the length of a collection.

In JSP 1.x, not so much...
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JSTL1.0.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean I have to mix java scriplet with JSTL ?

like

<%
int length = array.length;
pageContext.put("length", length);
%>
<c:forEach ....>
<c:if test="${index < length">
</c:forEach>

Sounds like NOT work.

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of JSP are you using? Please don't make me ask again.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSTL 1.0, JSP 2.0
Thanks
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using JSP 2.0, you need to be using JSTL 1.1. Otherwise, the JSTL and the JSP engine will have fights over who's job it is to evaluate the EL.

Replace your JSTL 1.0 jars with JSTL 1.1, update your JSTL URIs and you'll be able to use the JSTL functions, including fn:length().
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, how to confirm what version of JSTL, JSP we are using ? Do we have DOS command to do this ?

My web server is Tomcat 5.0, it should use JSP 2.0, right ?

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Tomcat 5 is JSP 2.0-capable and will run as a JSP 2.0 engine if the web app is declared as a Servlets 2.4 app in the deployment descriptor.

What URIs are you using for the JSTL? They differ between JSTL 1.0 and 1.1.
[ July 13, 2006: Message edited by: Bear Bibeault ]
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Bear,

My web.xml declares

It means, it is using JSP 1.0, right?

in JSTL, declares

Here, I am confusing JSTL version. Sounds like it is using JSTL 1.0, but why it is using 'web-jsptaglibrary_1_2.dtd' ?

Thanks.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Here, I am confusing JSTL version. Sounds like it is using JSTL 1.0,
> but why it is using 'web-jsptaglibrary_1_2.dtd' ?
That is correct. The DTD is defined as part of the JSP spec. JSTL1.0 was written for JSP1.2 - it is a custom tag library for JSP1.2.

Your web.xml is declared as using Version 2.3.
You have JSTL 1.0 installed.
So your web application will act like a JSP1.2 container - ie EL will only be evaluated in the JSTL tags. Also you can't use EL functions.

If you want to use JSP2.0/JSTL1.1, you need to update your web.xml to declared itself as version2.4, and also install JSTL1.1 jars instead of JSTL1.0.

As an aside, the code will work perfectly fine without that if statement.
Just try it:

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:This test page:



should give you enough clues to figure out your answer on your own.



Hi Bear, I know you posted this in 2006 but it just helped me figure out how to access array indexes using the jstl foreach loop! Thank you very much
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure!
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic