Struts <logic:iterate> & hasNext ???

  • Thread starter Mark van de Veerdonk
  • Start date
M

Mark van de Veerdonk

Hi,


I'm new to Struts and I'm looking for a feature I just can't find. Is there
a way to test within a <logic:iterate> loop whether the current item is the
last item or not? I want to create a comma seperated list that will be the
initialization of a javascript array. So there must be no comma after the
last entry.

I can't find a <logic:hasnext> or something like that. Is a tag like that
available in the Open Source community? I'm using 100% scriplet code now
(including not using <logic:iterate> anymore), which is yucky of course. It
would be somewhat less yucky if I could still use <logic:iterate> and access
the Iterator that <logic:iterate> creates internally to perform the
..hasNext() in scriplet code. Is this possible

What is the best way to go about this? Thanx!


Mark
 
S

Sudsy

Mark said:
Hi,


I'm new to Struts and I'm looking for a feature I just can't find. Is there
a way to test within a <logic:iterate> loop whether the current item is the
last item or not? I want to create a comma seperated list that will be the
initialization of a javascript array. So there must be no comma after the
last entry.

So why not approach it from the other end? Create an empty string and
each time through the iterator you add a comma if the string length is
greater than 0. Something like this:

String s = "";
while( iterate ) {
if( s.length() > 0 )
s += ",";
// append token from iterator
}
 
T

Tim Slattery

Mark van de Veerdonk said:
Hi,


I'm new to Struts and I'm looking for a feature I just can't find. Is there
a way to test within a <logic:iterate> loop whether the current item is the
last item or not? I want to create a comma seperated list that will be the
initialization of a javascript array. So there must be no comma after the
last entry.

Not with the Struts logic collection, but look at the forEach operator
in JSTL. It makes an object available that contains lots of
information about the iteration, including what you need. The JSTL
library is included in the Struts distribution.
 
S

Sudsy

Mark said:
Hi,


I'm new to Struts and I'm looking for a feature I just can't find. Is there
a way to test within a <logic:iterate> loop whether the current item is the
last item or not? I want to create a comma seperated list that will be the
initialization of a javascript array. So there must be no comma after the
last entry.

Something I just found in the API javadocs: the bean:size tag.
It sets a java.lang.Integer but you could use it for your aims.
 
M

Mark van de Veerdonk

Sudsy said:
Something I just found in the API javadocs: the bean:size tag.
It sets a java.lang.Integer but you could use it for your aims.

Together with <logic:equals> (or something like that) that should work.
Thanx. But it still seems a bit like a workaround to me. In my opinion,
there should be something like <logic:hasNext>. As I'm still looking around
for the right tools/technology to work with, I'm thinking of leaving JSP and
moving to FreeMarker. I appears to me like it has quite a lot of advantages
over JSP.


Mark
 
S

Sudsy

Mark said:
Together with <logic:equals> (or something like that) that should work.
Thanx. But it still seems a bit like a workaround to me. In my opinion,
there should be something like <logic:hasNext>. As I'm still looking around
for the right tools/technology to work with, I'm thinking of leaving JSP and
moving to FreeMarker. I appears to me like it has quite a lot of advantages
over JSP.

Mark

It seems a bit extreme to abandon a widely utilized technology
merely because it doesn't natively support a particular function
you had in mind. Write your own custom tags (or contract for the
services of a professional such as myself) or try to find some
on the 'net which do what you want. I highly recommend Advanced
JavaServer Pages by David M. Geary (ISBN 0-13-030704-1) if you
want to learn how to program custom tags.
YMMV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top