Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use c:forEach tag inside another c:forEach tag

 
Geraldine, Wang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've got en error when I tried to use <c:forEach> inside another <c:forEach> to display a vector of String arrays.

The error message is:
javax.servlet.ServletException: The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.

The concerned section of the javabean mybean.java is:

//connect to database to get the resultset
...
ResultSet rs = cst.getResultSet();
if (rs != null) {
myVector = new Vector();
while (rs.next()) {
String myArray[] = new String[10];
myArray[0]=rs.getString(1);
myArray[1]=rs.getString(2);
...
myArray[9]=rs.getString(10);
myVector.addElement(myArray);
}
rs.close()
}
...

The concerned jsp calling the bean is:

<jsp:useBean id="theBean" scope="session" class="mybean" />
<jsp:setProperty name="theBean" property="..." />
<%
...
Vector myVector=theBean.getResultFromDatabaseIntomyVector();
pageContext.setAttribute("myVector",myVector);
%>
<table>
<c:forEach var = 'vectorItem' items='${myVector}'>
<tr>
<c:forEach var = 'arrayItem' items='${vectorItem.value}'>
<td><c ut value='${arrayItem}'/></td>
</c:forEach>
</tr>
</table>

Can anyone show me what's the right jsp for displaying a vector of String arrays?

Thanks in advance.
Geraldine
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Geraldine Geraldine",

There aren't may rules that have been put into place here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Geraldine, Wang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've got the answer to this problem already. The correct jsp bit should be:

<table>
<c:forEach var = 'vectorItem' items='${myVector}'>
<tr>
<c:forEach var = 'arrayItem' items='${vectorItem}'>
<td><c out value='${arrayItem}'/></td>
</c:forEach>
</tr>
</c:forEach>
</table>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic