Results 1 to 7 of 7

Thread: forEach not reading a List

  1. #1
    Join Date
    Oct 2006
    Location
    San Francisco
    Posts
    7

    Default forEach not reading a List

    I've got a Map containing a List of Users given to my jsp. I am trying to iterate through them with a <c:forEach>, and it doesn't like it. My User object is a simple JavaBean. I've used <c:out> on the List within the map and I get the data. Why isn't the <c:forEach> working?

    SpringappController.handleRequest()
    Code:
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Map data = new HashMap();
    
            List users = getUserManager().getUsers();
            data.put("users", users);
            logger.info("Users: " + users);
    
            return new ModelAndView("hello", "data", data);
        }
    jsp
    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
    
    <html>
    <body>
    <c:forEach items="${data.users}" var="user">
      <c:out value="${user}"/><br/>
    </c:forEach><br/>
    
    <c:out value="${data.users}"/><br/>
    
    <c:out value="${data.users[0].username}"/> - <c:out value="${data.users[0].age}"/><br/>
    <c:out value="${data.users[1].username}"/> - <c:out value="${data.users[1].age}"/><br/>
    <c:out value="${data.users[2].username}"/> - <c:out value="${data.users[2].age}"/><br/>
    </body>
    </html>
    output
    HTML Code:
    <html>
    <body>
    
      ${data.users}<br/>
    <br/>
    
    [business.User@18f1be9, business.User@717d91, business.User@eafb71]<br/>
    
    Matt - 27<br/>
    Jonathan - 24<br/>
    
    Zac - 19<br/>
    </body>
    </html>

  2. #2

    Default

    I don't really see why it is outputing ${data.users}?

    The code looks right to me as long as there is a toString() common override on your User class.

    what happens when you do

    Code:
    <c:forEach items="${data.users}" var="user">
      <c:out value="${user.username}"/><br/>
    </c:forEach><br/>
    ?
    Last edited by whipshaw; Oct 6th, 2006 at 09:11 PM. Reason: FIX
    POJB - Plain Old Java Blog
    http://jroller.com/page/whipshaw

  3. #3
    Join Date
    Oct 2006
    Location
    San Francisco
    Posts
    7

    Default

    I get a blank string. And only one at that, when there are three users in the list.

  4. #4
    Join Date
    Oct 2006
    Posts
    3

    Default

    FYI, I'm seeing the exact same behavior--first foray into Spring. I'm getting it trying to set up the tutorial code (http://www.springframework.org/docs/...-by-step.html). No resolution yet, but I'll let you know if I figure it out...

  5. #5

    Default

    The best solution: Switch to using FreeMarker
    POJB - Plain Old Java Blog
    http://jroller.com/page/whipshaw

  6. #6
    Join Date
    Oct 2006
    Location
    San Francisco
    Posts
    7

    Default

    My latest experiments

    Code:
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            ModelAndView mav = new ModelAndView("hello");
    
            mav.addObject("now", new Date().toString());
            mav.addObject("usernames", userManager.getUsernames());
    
            return mav;
        }
    HTML Code:
      <h2>Now</h2>
      <c:out value="${now}"/>
    
      <h2>Usernames</h2>
      <ul>
      <c:forEach items="${usernames}" var="username">
        <li><c:out value="${username}"/></li>
      </c:forEach>
      </ul>
    
      <h2>Usernames array</h2>
      <c:out value="${usernames}"/>
    
      <h2>Numbers</h2>
      <c:forEach var="i" begin="3" end="20" step="3">
        <c:out value="${i}"/><br/>
      </c:forEach>
    Results:
    Now
    Tue Oct 10 16:31:48 PDT 2006

    Usernames
    * ${usernames}

    Usernames array
    [matt, ceme, kid, senior]

    Numbers
    3
    6
    9
    12
    15
    18
    As you can see the <c:out> is working for the date ("Now").
    The username array is in the jsp ("Username array"). Those are my four users.
    The <c:forEach> isn't a completely useless tag ("Numbers").
    But the <c:forEach> won't list out the usernames.

    I'm using the Spring 2.0 Framework, but I don't think that matters.

    Maybe I'll just be a fireman.

  7. #7

    Default

    Quote Originally Posted by Checkerbelt View Post
    Maybe I'll just be a fireman.
    lol...

    I'm sorry to hear you're still stuck on this... Well, if you want to get ambitious and switch to Freemarker (that's what I use), the equivilant code would look like this:

    Code:
    <h2>Now</h2>
      ${now}
    
      <h2>Usernames</h2>
      <ul>
      <#list usernames as username>
        <li>${username}</li>
      </#list>
      </ul>
    
      <h2>Usernames array</h2>
      ${usernames}
    
      <h2>Numbers</h2>
      <#list 3..20 as i>
        ${i}"<br/>
      </#list>
    Isn't that nicer, cleaner, and simpler... AND IT WILL WORK!

    My company switched from JSP to FreeMarker and it was one of the best decisions we ever made...

    By the way, I don't think your JSP code is wrong, you are using the forEach expression correctly. I'm thinking it is a configuration problem. You should check that you are using the latest JSTL library and schema is configured correctly.

    I did a quick search and came across someone else that seemed to be having similiar problem. http://www.theserverside.com/discuss...hread_id=32113

    If you still can't get it to work, switch to a cleaner, easier, faster (to code) view technology... Like FreeMarker... Trust me, you won't get these types of errors and the error messages are much nicer...
    POJB - Plain Old Java Blog
    http://jroller.com/page/whipshaw

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •