Discussions

Web tier: servlets, JSP, Web frameworks: need help w/ replacing JSP scriptlet with JSTL & EL

  1. Can anybody tell me a full, JSTL equivalent of this block of JSP? I'd like to get rid of the scriptlet line.

    <c:forEach items="${requestScope.attributeNames}" var="attName">

    <% pageContext.setAttribute("itemList", request.getAttribute((String)pageContext.getAttribute("attName"))); %>

    <c:forEach items="${itemList}" var="item"><%-- display the item --%></c:forEach>

    </c:forEach>

    Essentially, I have a collection of attribute names stored in the request. Also in the request is a Collection of Items stored under each attribute name. First, I'm iterating through my attribute names. Then I want to pull the Collection out of the request stored under each name and iterate through that, too.
  2. It would be simpler if the top level collection contained the actual sub-collections rather than their names then you could simply use a nested c:forEach :-)

    Anyway this does it with the setup I think you have:

        <c:forEach items="${requestScope.namesList}" var="name">
          <c:out value="${name}"/> List<br>
           <c:forEach items="${requestScope[name]}" var="inner">
              <c:out value="${inner}"/><br>
           </c:forEach>
        </c:forEach>

    Where the setup for the lists was:
        ArrayList namesList = new ArrayList();
        namesList.add("FOO");
        namesList.add("BAA");
        request.setAttribute("namesList",namesList);
        
        ArrayList fooList = new ArrayList();
        fooList.add("foo1");
        fooList.add("foo2");
        fooList.add("foo3");
        request.setAttribute("FOO",fooList);
            

        ArrayList baaList = new ArrayList();
        baaList.add("baa1");
        baaList.add("baa2");
        baaList.add("baa3");
        request.setAttribute("BAA",baaList);
  3. Double score[ Go to top ]

    Thanks! You're right on both counts. I'm taking your first advice, though, and switching to a Map of List objects (I actually need a key name for each List, and the Map gives me that as opposed to a List of Lists).

    <c:forEach items="${requestScope.myMap}" var="mapEntry">
    <c:out value="${mapEntry.key}"/>
    <c:forEach items="${mapEntry.value}" var="listItem">
    ...
    </c:forEach>
    </c:forEach>