This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

apache velocity #foreach loop

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to iterate through a string array, also want to print the iteration number,

output should be,
1. Not
2. my
3. fault

but when it produces,
${foreach.count} . Not
${foreach.count} . my
${foreach.count} . fault

Anyone can tell what's going wrong here ? on server console I get
INFO: Velocity [debug] Null reference [template '/Test.vm', line 3, column 13] : $foreach.count cannot be resolved.

Thanks
Bharat
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried "$foreach.count" instead of "$!{foreach.count}"?
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Ulf, I tried $foreach.count then it produces
$foreach.count . Not
$foreach.count . my
$foreach.count . fault

saw some stringe behavior when I use $!{foreach.count} output is

. Not
. my
. fault


~Bharat
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version? Is foreach's scope control set to false?
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using velocity-1.6.1.jar,velocity-tools-*-1.4.jar.
velocity.properties is

resource.loader = file
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = D:/eclipse/workspace/manager/WebContent/vm
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 2

Thanks,
Bharat
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally

$velocityCount is the answer.

Thanks verybody.
~Bharat
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know its beena while since the last reply on this thread, but i'll try my luck anyway -

I'm using velocity templates in my spring app n have some simple templates working successfully. Like passing a username and the reset password to the email body etc.
my next task is to learn how to pass a dynamic list of objects into the template.
say, for example, i will pass to the template an object array that contains the names of members. it may be 3 objects once and 5 the next.
i cannto seem to figure out a way to do this considering its my second day on velocity :-) can anyone please advise

cheers
p
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prashanth try this

prepare you template say userTemplate.vm


you have to put userList object in velocity context. for this do the following




 
prashanth talkad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Bharat. Cheers - prashanth
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!