JSTL <c:forEach> tag problems.

I am having problems with tag in my application.  Firstly in my action class (struts BTW) i call a function that returns a list of Entry objects from a  DB

   request.setAttribute("entries", entries);

On the next JSP page i am doing this

            <c:forEach var="ent" items="${entries}" varStatus="status">
                    <c:when test="${status.count % 2 != 0}">
                        <tr class="oddRow">
                        <tr class="evenRow">
                        <c:url value="goToUpdateForm" var ="url">
                            <c:param name="id" value="${ent.teamId}"/>
                            <c:param name="dispatch" value="goToUpdateForm"/>
                       <a href ='<c:out value="${url}"/>'><c:out value="${ent.teamId}"/></a>
                    <td><c:out value="${ent.eventClass}"/></td>
                    <td><c:out value="${ent.nameOne}"/></td>
                    <td><c:out value="${ent.nameTwo}"/></td>
                    <td><c:out value="${ent.location}"/></td>

But this will not run properly and i end up getting the error

   Unable to find a value for "teamId" in object of class "java.lang.String" using operator "."

I know that the c:forEach is treating this list as a list of strings and therefore failing but i can't seem to find out how to how to tell my app to treat this as a list of entries.

Any ideas?


Who is Participating?
bloodredsunConnect With a Mentor Commented:
Make sure that you have a method in Entry called getTeamId() for this to work.

here's a proof of concept that may help you:

<%@ page import="java.util.*, com.bloodredsun.examples.Person" %>
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<head><title>JSTL c:forEach example</title></head>

ArrayList arrList = new ArrayList() ;
arrList.add( new Person("friend" , 30) ) ;
arrList.add( new Person("enemy" , 34) ) ;
arrList.add( new Person("mother" , 50) ) ;
arrList.add( new Person("father" , 55) ) ;

request.setAttribute("myList", arrList );


<c:forEach var='alItem' items='${requestScope.myList}' varStatus='status'>
      Status:<c:out value='${alItem.status}' />
      Age:<c:out value='${alItem.age}' />
      Count: <c:out value='${status.count}'/>
      <br />

package com.bloodredsun.examples;

import java.util.HashMap;

public class Person {
      public Person(String string) {

      public Person(){}
      public Person(String pStatus, int pAge){
            setStatus( pStatus ) ;
            setAge( pAge ) ;
      private String status ;
      private int age ;

      public String getStatus() {
            return status;

      public void setStatus(String pStatus) {
            status = pStatus;

      public int getAge() {
            return age;

      public void setAge(int pAge) {
            age = pAge;

This outputs
 Status:friend Age:30 Count: 1
Status:enemy Age:34 Count: 2
Status:mother Age:50 Count: 3
Status:father Age:55 Count: 4

as it successfully iterates over the ArrayList.
If you've set the object in the request scope you'll need to call  

<c:forEach var="ent" items="${requestScope.entries}" varStatus="status">

else you are calling it from the pageContext...
phuzzyAuthor Commented:
Nope that didn't make a difference, still getting the same error.

The thing is it's finding the list but treating it as a list of strings and therefore printing out the pointer information to the objects.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Hi James,

Are you sure that you are passing the right type of class? It seems that you are passing a String instead of List or etc.

Hi James,

Please post how do you query your "entries" in your java class.

phuzzyAuthor Commented:
Sorry for the delay on this.  I have been away for the weekend.

By "query" do you mean how i get it from the DB?  If so i somply call my Marathon Service whihc uses iBatis that returns a list of entry objects.  I can access and manipulate this list through other methods but i can't access it via this JSP page.

Is that enough info. I can post more source when i return to work tomorrow.

phuzzyAuthor Commented:
It is definaltey a list of entry objects as I also used a custom tag that calls the Entry Service and outputs the list which works fine.
phuzzyAuthor Commented:

Just tried replacing c:forEach with a logic:iterate and got a similar error,

javax.servlet.jsp.JspException: Cannot find bean [com.mourne2day.Entry@a56214, com.mourne2day.Entry@1ca1588, com.mourne2day.Entry@4bb369] in any scope.

Hope this helps.
All Courses

From novice to tech pro — start learning today.