• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Iteration in structs

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We can iterate a collection or array using iterate tag of struts. But my requirement is bit different. I have an integer variable and i want to execute a loop that much of time.

ie Let I had a variable stud = 5
I want to print 5 textboxes ie according to the value of stud.
Is there any way using struts tag or I need to add a normal for loop in scriplets?

Also which is the efficient way to take values from these dynamic textboxes?

Thanks inb Advacne.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use jstl tags like this
<c:forEach var="i" begin="1" end="5">

</c:forEach>

also use indexed properties for your text box.
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok the tag you given is working,
but one doubt, how can i acces the counter variable of this loop.
I tries the following, but doesn't work

<c:forEach var="i" begin="1" end="5">
Iteration <%=i%>
</c:forEach>
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do it like this:

Note: if you're using a Servlet 2.4 containter (such as Tomcat 5) you can just specify ${status.index} without the enclosing <c:out> tag.
[ February 08, 2007: Message edited by: Merrill Higginson ]
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is giving output as

Iteration ${status.index} Iteration ${status.index} Iteration ${status.index} Iteration ${status.index} Iteration ${status.index}
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is most likely because you haven't configured your web application as a Servlet 2.4 application. Check which XML Schema you reference in your web.xml file.

The easy way to fix this is just to leave your application at Servlet 2.3 and use the <c:out> tag.

If you want to upgrade your application to Servlet 2.4, there's More information on what to do at this link. Since you will be using a different version of JSTL, you will also need to change your taglib definitions as per this link. This link also directs you to where you can download the JSTL 2.0 jars if you need them.
[ February 09, 2007: Message edited by: Merrill Higginson ]
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I used <c ut> tag.


<c:forEach varStatus="status" begin="1" end="5">
Iteration <c ut value="${status.index}" />
</c:forEach>

But result is
Iteration ${status.index} Iteration ${status.index} Iteration ${status.index} Iteration ${status.index} Iteration ${status.index}
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what to tell you, except that you're obviously doing something wrong. Did you remeber to include the taglib definition?

The only other thing I can think of is that maybe your JSTL jar files are old or bad. Try re-downloading them. One of my previous posts has the link.

I just put together a quick JSP and put it in a Servlet 2.4 application:



When I run it, here's the output I get:

Index demo

0
1
2
3
4
[ February 12, 2007: Message edited by: Merrill Higginson ]
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the code given by you. But I am getting
${status.index}
${status.index}
${status.index}
${status.index}
${status.index}

So I think this is the mistake in my web.xml.

Can you specify the changes I need in my web.xml.

 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link explains how to modify your web.xml file. Don't forget that in a Servlet 2.4 web.xml, the <taglib></taglib> tags must all be enclosed in a single <jsp-config></jsp-config> stanza.
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edit my web.xml as it is given in the link.
Also give <taglib> tag inside <jsp-config>

But still the same thing happens.
I am using tomcat 5.
Is there any need of additional or new versions of jar files.
 
Vani Bandargal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you try this:

<c ut value="${status.count}" />


[ February 13, 2007: Message edited by: Vani D Bandargal ]
[ February 13, 2007: Message edited by: Vani D Bandargal ]
 
Jobin Mathew
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, It worked.

What is the difference between status.index & status.count?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that you know the basics, I'd say it's time to down load the JSTL 1.1 Specification and read about the fine points yourself.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic