[thelist] Java/JSP puzzler revisited (simplified) - almost there!

Tom Dell'Aringa pixelmech at yahoo.com
Thu Oct 20 14:57:13 CDT 2005


--- Hassan Schroeder <hassan at webtuitive.com> wrote:
> D'oh, my bad -- yes, I'm using a variable before it's defined. You
> could put it (and the other initial def) up higher, like:
> 
> 	if (!verticalNodes.hasNext()) return;
> 	int level = 1;
> 	int previousLevel = 1;
> 	/* that keeps the initial definitions outside the loop */
> 	...
> 	
> 	previous level = level;
> 	level = node.getLevel();
> 
> Try that and see what happens :-)

Ahhh really close! Those fixed the earlier problems I get output that technically looks correct.
The only issue is I seem to be getting a double ending LI like so:

</li></li>

After each list item. And when a UL starts, it must start in an OPEN LI like so:

<li>blah blah
   <ul>
     <li>blah</li>
   </ul>
</li>

etc

I could probably live with it if it closes the top LI but still puts the nested UL in the same
place. I'll play with it some myself but if anyone has any other ideas...

Thanks a BUNCH Hassan! For review, here is the current iteration of the code:

<%
	ListIterator verticalNodes = nodesInVerticalOrder.listIterator();
	if (!verticalNodes.hasNext()) return;
	int level = 1;
	int previousLevel = 1;

	StringBuffer dataCell = new StringBuffer();
	StringBuffer javaScript = new StringBuffer();
	
	// start UL
	dataCell.append("<ul>");

	// iterate through each nav item and write it out
	while (verticalNodes.hasNext()) {

		MenuItemNode node = (MenuItemNode) verticalNodes.next();

		previousLevel = level;
		level = node.getLevel();  // # from 1-4
		
		if ( verticalNodes.hasPrevious() )
		{
			dataCell.append("</li>");
		}
		if ( level > previousLevel )
		{
			dataCell.append("<ul>");
		}
		if ( level < previousLevel )
		{
			dataCell.append("</ul>");
		}
	
		// begin new list item, write out class name and link
		dataCell.append("<li><a href=\"" + node.getHref() + "\"");
		//close the href, write the anchor text, close the anchor.
		dataCell.append(">" + node.getTitle() + "</a>");
		// end of list item and link

		//close the LI item
		dataCell.append("</li>\n");
	}
	// close out the UL
	dataCell.append("</ul>");
%>

THanks,

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