tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shawn Bayern <bay...@essentially.net>
Subject Re: Looping a tree-structure of objects of unknown depth
Date Fri, 14 Feb 2003 14:19:11 GMT
On Fri, 14 Feb 2003, Sakke Wiik wrote:

> I have a tree-structure of objects similar to a filesystem, where
> directories has subdirectories to an unknown depth. How can i loop and
> display all these in jsp without knowing the depth of the tree? it's
> easy to do in pure java with a method calling itself, but I can't use
> methods inside a jsp, or can I?

You could define a method with a <%! ... %> declaration and use
scriptlets; Java and JSP are really the same thing.

But as a more interesting approach, you can think of each JSP page as a
method or component; you can write a JSP page to handle each directory and
then "call" it recursively with <jsp:include> each time you encounter a
directory.

In other words, for a hypothetical "file" object with boolean properties
like isFile() and isDirectory(), you could do the following:

  printDirectory.jsp
  ----------------------
  <c:forEach value="${requestScope.currentDir}" var="f">
    <c:choose>
      <c:when test="${f.file}">
        <c:out value="${f.name}" /> <br />
      </c:when>
      <c:when test="${f.directory}">
        <table>
          <tr>
            <td>
	      <b><c:out value="${f.name}" /></b> <br />
              <jsp:include page="printDirectory.jsp" />
            </td>
          </tr>
        </table>
      </c:when>
    </c:choose>
  </c:forEach>

Cute, huh? :-)

-- 
Shawn Bayern
"JSTL in Action"   http://www.manning.com/bayern


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Mime
View raw message