DisplayTag inside a loop

In my (brand) new job, I’ve developed a simple report that is a list of employers along with phone calls they made. The employer list is iterated using JSTL’s <c:forEach> and the call list is rendered by DisplayTag.

Very simple, except because I got a little problem: DisplayTag requires an unique id (set by the “uid” attribute) and this uid is also the iteration variable - just like the “var” attribute in <c:forEach>.

This means I need to create an unique id for every outer iteration. But this also means that I create a new “current inner item” variable for each outer item. If I use a non-unique id, the navigation of all tables will be the same: if I click “next page” on the first table, the second table will also go to the next page. If I define an uid based on outer item data, I cannot access it using a direct approach (like <c:out>), because the EL must change for every iteration.

This limitation was solved by predefining the uid (before the DisplayTag table):

<c:forEach items="${myList}" var="${myItem}">
  <%-- This creates an unique id based on item's id --%>
  <c:set var="tableId" value="list-${myItem.id}"/>

  <display:table uid="${tableId}" name="${myItem.mySubList}">
    <display:column property="mySimpleValue"/>
    <display:column>
      <%-- Now, a little EL magick --%>
      <my:tag value="${pageScope[tableId].myValue}"/>
    </display:column>
  </display:table>
</c:forEach>

Notice that this approach isn’t needed if you want to show simple values, like a String. Using this to pass the value to a <c:out> is a waste. But it is the only way to work if you need to apply any transformation - like formatting a Date.

Tags: , , ,

One Response to “DisplayTag inside a loop”

  1. Extreme Java » Blog Archive » JAAS + Struts + Tomcat = Secure and pratical WordPress 2.3.3 Says:

    […] Still working on the application I describe in my previous post, I started to integrate it to use JAAS. […]

Leave a Reply