[thelist] Java/JSP puzzler revisited (simplified)
Tom Dell'Aringa
pixelmech at yahoo.com
Thu Oct 20 11:35:09 CDT 2005
Ok, I think my post yesterday was too convoluted. I've worked on it a lot more (without luck) but
I have simplified it a great deal. What I need to do is write out a nested UL (with UL's inside)
down to 4 levels. I have a JSP with java code inside that controls this page. I cannot change the
way the page is delivered - it has to use this code fragment.
The comment block near the bottom of the code is the area where I think the solution needs to be,
I just can't figure out how to make it work. My thought is that it must recurse somehow. Here is
the code:
---------------------------------------------------------
<%
Iterator verticalNodes = nodesInVerticalOrder.iterator();
if (!verticalNodes.hasNext()) return;
StringBuffer dataCell = new StringBuffer();
// start root UL
dataCell.append("<ul>");
// iterate through each nav item and write it out
while (verticalNodes.hasNext()) {
MenuItemNode node = (MenuItemNode) verticalNodes.next();
int level = node.getLevel(); // # from 1-4 always have access to this for each LI
// begin new list item, write out LI and anchor
dataCell.append("<li class=\"" +className+ "\">");
dataCell.append("<a href=\"" + node.getHref() + "\">" + node.getTitle() + "</a>");
/****
Herein is the area where the problem lies.
It is here that I must somehow open a new UL for each
level, then write all the LIs of that level inside it,
then close it. I must be able to do this for levels 2,3 and 4.
The UL must remain open as long as I'm in a 2-4. At the end of a
2-4, it must close. Inside THOSE, it must do the same for 2-4, and it must
jump out for a level 1. All the while the HTML must come out
structured correctly (see below).
****/
//close the LI item
dataCell.append("</li>\n");
}
// close out the root UL
dataCell.append("</ul>");
%>
The output should look like this:
<ul>
<li>level one</li>
<li>level one</li>
<li>level one</li>
<li>level one
<ul>
<li>level two</li>
<li>level two
<ul>
<li>level three
<ul>
<li>level four</li>
<li>level four</li>
<li>level four</li>
<li>level four</li>
</ul>
</li>
<li>level three</li>
<li>level three</li>
<li>level three</li>
</ul>
</li>
<li>level two</li>
<li>level two</li>
</ul>
</li>
<li>level one</li>
<li>level one</li>
<li>level one</li>
</ul>
The tricky part is writing in all the sub levels before the LI closes (since a sub UL must reside
inside the LI to be valid HTML) - and having it go from levels 1, 2, 3 - 4 and back out to 1.
Anyone have any ideas?
Tom
http://www.pixelmech.com/
A man spoke frantically into the phone: "My wife is pregnant and her contractions are only two minutes apart"! "Is this her first child?" the doctor asked. "No, you idiot!" the man shouted. "This is her husband!"
Q: What do you call a muddy chicken who crossed the road two times?
A: A dirty double crosser...
More information about the thelist
mailing list