How to display elements of a collection in JSP using Core Tag Library
I have a JSP where I am trying to display the elements of a collection. This collection is set as as Request Attribute in the action class. This collection contains the result of a database query done using Toplink. I am getting the collection as Not Null in the JSp but I am not able to display its contents using the following code. It just displays the balnk rows but I know the data is there in the collection when I debug it.
And when I try to display ${not empty cont} I get the value true on JSP. But the rows are displayed empty.
Can anybody pls guide me where I am going wrong:
(col1, col2....) are the column names which are returned by the query.
it is returned in some back end class as:
Collection contAll= (Collection) session.executeQuery(query);
and in the action class I set this returned value as the request attribute as:
request.setAttribute("cont", contAll);
</tr>
<c:forEach var="c" items="${cont.rows}">
<tr class="rowdata">
<td>
<c:choose>
<c:when test="${not empty c.col1}">
<b><c:out value="${c.col1}"/></b>
</c:when>
<c:otherwise>
<font color="red">No Col1#</font>
</c:otherwise>
</c:choose>
(<c:out value="${c.col2}"/>)
<c:out value="${c.col3}"/>
</td>
<td>
<c:out value="${c.col4}"/>
</td>
<td>
<c:out value="${c.col5}"/>
</td>
<td>
[1521 byte] By [
Rhea1978a] at [2007-11-27 11:18:41]

# 1
Hi
I thought the previous mail wasn't formatted so i did not get any suggestions, so just made it readable
I have a JSP where I am trying to display the elements of a collection. This collection is set as as Request Attribute in the action class. This collection contains the result of a database query done using Toplink. I am getting the collection as Not Null in the JSp but I am not able to display its contents using the following code. It just displays the balnk rows but I know the data is there in the collection when I debug it. And when I try to display ${not empty cont} I get the value true on JSP. But the rows are displayed empty. Can anybody pls guide me where I am going wrong: (col1, col2....) are the column names which are returned by the query. it is returned in some back end class as:
Collection contAll= (Collection) session.executeQuery(query);
and in the action class I set this returned value as the request attribute as:
request.setAttribute("cont", contAll);
In JSP
</tr>
<c:forEach var="c" items="${cont.rows}">
<tr class="rowdata">
<td>
<c:choose>
<c:when test="${not empty c.col1}">
<b><c:out value="${c.col1}"/>
</b> </c:when>
<c:otherwise>
<font color="red">
No Col1#</font>
</c:otherwise>
</c:choose>
(<c:out value="${c.col2}"/>)
<c:out value="${c.col3}"/>
</td> <td>
<c:out value="${c.col4}"/>
</td> <td>
<c:out value="${c.col5}"/> </td> <td>
# 2
What does the class you are accessing look like?
Does it have appropriate getters/setters?
${c.col1} translates as c.getCol1() is that method there?
What does the object stored in the list look like?
# 3
I have an arraylist containing Maps.
Now I set this array list in the request attribute
How do I access the elements of this arrayList 's Map in JSP:
with core tag LIbrary
# 4
${Map.key} is not displaying anything
# 5
[nobr]Based on this example:
MyBeanpublic List< Map<String, String> > getArrayList() {
List< Map<String, String> > arrayList = new ArrayList< Map<String, String> >();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("key1", "value1a");
map1.put("key2", "value2a");
arrayList.add(map1);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("key1", "value1b");
map2.put("key2", "value2b");
arrayList.add(map2);
return arrayList;
}
There are 2 ways.
1) If you know the key names before:<c:forEach items="${myBean.arrayList}" var="hashMap">
key1 value: <c:out value="${hashMap.key1}" />
key2 value: <c:out value="${hashMap.key2}" />
<br />
</c:forEach>
2) If you don't know the key names before and just want all entries:<c:forEach items="${myBean.arrayList}" var="hashMap">
<c:forEach items="${hashMap}" var="entry">
key: <c:out value="${entry.key}" />
value: <c:out value="${entry.value}" />
<br />
</c:forEach>
<br />
</c:forEach>
Note: remove spaces from generic type parameter if you're about to copypasting this. This forum doesn't parse nested < and > tags correctly.[/nobr]
# 6
Thankx..
But is it necessary to define a bean without a bean is it possible suppose in some action class
# 7
It was just a basic example. I wanted to show how the arraylist is populated, because the 1st example shows how to get the values by keys.
# 8
<c:when test="${not empty n1Collection.nonAssigned}"> is showing false where as the size of this n1Collection is 5.
In the action class I set the values as
Collection n1Collection=new ArrayList();
nonAssigned.put("A",s1);
nonAssigned.put("B",s2);
n1Collection.add(nonAssigned);
# 9
c:when test="${not empty a.nonAssigned}"> is showing false where as the size of this n1Collection is 5.
In the action class I set the values as
Collection n1Collection=new ArrayList();
nonAssigned.put("A",s1);
nonAssigned.put("B",s2);
n1Collection.add(nonAssigned);
request.setAttribute("a",n1Collection)
gives the error:
An error occurred while evaluating custom action attribute "test" with value "${not empty a.nonAssigned}"> 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. (null)' javax.servlet.jsp.JspTagException: javax.servlet.jsp.JspException
# 10
The ArrayList class doesn't know the method getNonAssigned(), nor the method get("nonAssigned"). Remove that ".nonAssigned" part in the EL and use an integer index instead, a[0] for example, for the 1st item of the ArrayList.
# 11
removed it but I am not able to access wht values of the hashmap in the arrayList with the keys:
<c:forEach var="cont" items="${a}">
<tr class="rowdata">
<td>
<c:choose>
<c:when test="${not empty a.A}">
<b><c:out value="${a.A}"/></b>
# 12
Re-read my examples. You're completely misinterpreting how it works.
# 13
I add the HashMaps in a collectin c1.
Maps contain the keys as A, B
request.setAttribute("cont",c1);
Now in order to access the values in the maps in the collection I iterate over the collection and trying to use.
<c:forEach var="values" items="${cont}">
<c:out value="${values.A}"/></b>
what is wrong in this:
pls pls guide me
# 14
where collection c1=new arraylist();
and then i add the maps in c1