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

Tom Dell'Aringa pixelmech at yahoo.com
Fri Oct 21 07:34:20 CDT 2005


--- Hassan Schroeder <hassan at webtuitive.com> wrote:
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
> > Tom Dell'Aringa wrote:
> > 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>
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

> > This is hard without a runnable example, but try these changes :-)
> >
> > 		//if ( verticalNodes.hasPrevious() )
> >	        if ( verticalNodes.hasPrevious() && level == previousLevel )
> >
> 	/* I believe this next line is unnecessary -- since it's
> 	 * closed by the verticalNodes.hasPrevious() above...
> 	 * -- thought I deleted it in the sample sent off-list
> 	 */
> > 	     //	dataCell.append("</li>\n");
> > Try that and send me the output :-)

Ah, that was near perfect! The one thing I had left was that at the opening I was getting a
closing LI tag right after the UL, and subsequently not getting a closing LI on the last list item
of an UL.

I added a counter at the top, and if the counter was 1 - I didn't write out the closing LI. That
took care of the extra closing li at the start. Then, when closing the nested UL, I added another
closing LI right in front of it which took care of the other problem:

dataCell.append("</li></ul></li>");

At any rate, this now produces perfectly formed valid HTML nested ULs! Wheee-ooo

MANY thanks to Hassan for helping me with this - I was totally stuck. Here is the final script -
if anyone sees where I could improve anything let me know, otherwise I will run it as is:

-----
<%
 	ListIterator verticalNodes = nodesInVerticalOrder.listIterator();
 	if (!verticalNodes.hasNext()) return;
 	int level = 1;
 	int previousLevel = 1;
	int interationCount = 1;
 
 	StringBuffer dataCell = 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() )
	    if ( verticalNodes.hasPrevious() && level == previousLevel )
 		{
 			if( interationCount != 1 ) { dataCell.append("</li>"); }
 		}
 		if ( level > previousLevel )
 		{
 			dataCell.append("<ul>");
 		}
 		if ( level < previousLevel )
 		{
		/* add the post-ul-close /li here */
			dataCell.append("</li></ul></li>");
		}
 	
 		// 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>");
		interationCount++;
 	}
 	// close out the UL
	dataCell.append("</li>"); /* to close the final list item */
 	dataCell.append("</ul>");
%>
------

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