Constructing a directory tree with Java code implementation

Source: Internet
Author: User

How to use Java code to implement such a directory tree?

Create a data table first

Each data record its own ID and the ID of the parent node

Then go to the Java Code section:

 Publicstring Directory (string author) {StringBuffer treehtml=NewStringBuffer (); //get all the directory words (all fields included)list<tutorial> words =Bdexpandservice.getallwords (author);  for(inti = 0; I < words.size (); i++) {Tutorial Wordmap=Words.get (i); //System.out.println (wordmap);            if(Wordmap.getpid () = = 0) {treehtml.append ("<dl>"); //get the root directory ID, based on this ID to find all subdirectories of thisAppendtree (words, Wordmap, treehtml); Treehtml.append ("</dl>");        }} words.clear (); returntreehtml.tostring (); }    /*** * Construct directory tree < feature description > * *@paramTutorials *@paramTutorial *@paramtreehtml *@see[Class, Class # method, Class # member]*/    Private voidAppendtree (list<tutorial>words, Tutorial wordmap, StringBuffer treehtml) {        intTid =Wordmap.gettid (); //get the root directory ID, based on this ID to find all subdirectories of thismap<string, object> map =childtreehtml (words, TID); String nodehtml= Map.get ("treehtml"). toString (); BooleanHaschild = boolean.valueof (Map.get ("Haschild"). toString ()); if(haschild) {treehtml.append ("<dt class= ' node-close ' onclick= ' ShowTree ("). Append (Tid). Append (") '"); Treehtml.append ("Id= ' Tree_dt"). Append (Tid). Append ("' >"); Treehtml.append (Wordmap.getkeyword ()). Append ("</dt>");        Treehtml.append (nodehtml); } Else{treehtml.append ("<dt>"); Treehtml.append (Wordmap.getkeyword ()). Append ("</dt>"); }    }    /*** * Get subdirectories, construct directory tree < feature description > * *@paramTutorials *@paramtid *@return     * @see[Class, Class # method, Class # member]*/    PrivateMap<string, object> childtreehtml (list<tutorial> words,inttid) {Map<string, object> map =NewHashmap<string, object>(); StringBuffer treehtml=NewStringBuffer (); BooleanHaschild =false;  for(inti = 0; I < words.size (); i++) {Tutorial Wordmap=Words.get (i); intPID =Wordmap.getpid (); if(PID = =tid) {Haschild=true; Treehtml.append ("<dd name= ' Tree_dd"). Append (PID). Append ("'"); Treehtml.append ("Style= ' Display:none; ' > "). Append (" <dl> ").);                Appendtree (words, Wordmap, treehtml); Treehtml.append ("</dl></dd>"); }} map.put ("Treehtml", treehtml); Map.put ("Haschild", Haschild); returnmap; }
The string returned by the directory () method is the entire directory tree, which is then uploaded to the foreground page display, plus the CSS style.

Constructing a directory tree with Java code implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.