JSTL <c:forEach> tag problems.

Posted on 2005-04-22
Last Modified: 2008-02-01
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?


Question by:phuzzy
    LVL 29

    Expert Comment

    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...

    Author Comment

    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.
    LVL 16

    Expert Comment

    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.

    LVL 16

    Expert Comment

    Hi James,

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


    Author Comment

    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.


    Author Comment

    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.

    Author Comment


    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.
    LVL 29

    Accepted Solution

    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='' 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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The viewer will learn how to count occurrences of each item in an array.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now