469,338 Members | 8,115 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Two dimensional array + c:foreach

Hello

I have a servlet that creates results in a two dimentional array and then uses request.setAttribute to store it.

for example :
String[][] fullname = {{"name1", "surname1"},{"name2", "surname2"},{"name3", "surname3"}};

request.setAttribute("fullname",fullname);

Then JSP page will use JSTL c:foreach to display it:

<c:forEach var = "itemarr2" items = "${fullname}">
<tr>

<td>${itemarr2[0]}</td>
<td>${itemarr2[1]}</td>
</tr>
</c:forEach>

The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

Or may be another tag or method to solve this problem ?

Thank you
Lisa
Aug 17 '07 #1
3 10906
JosAH
11,448 Expert 8TB
Hello

I have a servlet that creates results in a two dimentional array and then uses request.setAttribute to store it.

for example :
String[][] fullname = {{"name1", "surname1"},{"name2", "surname2"},{"name3", "surname3"}};

request.setAttribute("fullname",fullname);

Then JSP page will use JSTL c:foreach to display it:

<c:forEach var = "itemarr2" items = "${fullname}">
<tr>

<td>${itemarr2[0]}</td>
<td>${itemarr2[1]}</td>
</tr>
</c:forEach>

The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

Or may be another tag or method to solve this problem ?

Thank you
Lisa
Well, you do know that itemarr2 is an array so you can use plain old Java to get
the number of elements in that array: itemarr2.length. You can use plain old
loops if you want or the JSTL loop.

kind regards,

Jos
Aug 17 '07 #2
Well, you do know that itemarr2 is an array so you can use plain old Java to get
the number of elements in that array: itemarr2.length. You can use plain old
loops if you want or the JSTL loop.

kind regards,

Jos

Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

Any examples on how to implement this is extremely appreciated.

Lisa
Aug 17 '07 #3
JosAH
11,448 Expert 8TB
Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

Any examples on how to implement this is extremely appreciated.

Lisa
I don't speak JSP JSTL tags, but I guess it can be something like this:

Expand|Select|Wrap|Line Numbers
  1. <c:forEach var = "name" items = "${itemarr2}">
  2. // do something with name here
  3.  
kind regards,

Jos
Aug 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Malcolm | last post: by
3 posts views Thread by Stefan Richter | last post: by
8 posts views Thread by Gopal Krish | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.