This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes jstl c:forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jstl c:forEach" Watch "jstl c:forEach" New topic

jstl c:forEach

liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
Hello, I have 2 string arrays (contact and contactid) that I want to use in a jstl c:forEach to produce select options. So far I have the code below - it puts contactid values into options value, but now how do I open a reference to contact array AND insert the data into options text? Thank you in advance.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60794

Assuming that both arrays are the same length, and that the order of their entries correpsond, you could do this by traversing one array and using the index to grab references from the other.

But, I don't do it this way. When passing information to the JSP to contruct an options list, I use a Map implementation where the entry key is used as the option value and the entry value as the display text. That way it's a better abstraction, and you only have to traverse one construct.

For example:

If there is a chance that either entry could contain characters needed encoding, you would also use <cut> to encode the values as you did in your original post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
This is great! Thanks a lot!
It is sorta covered in the JavaRanch Style Guide.
subject: jstl c:forEach
Similar Threads
Nested hashmap tags for dynamic pulldowns
assigning a function's result to a form field
Struts tags and JSTL
JSTL error
Regarding jstl issue