The most reliable Java recursive tree menu display

Source: Internet
Author: User

Write permission today to deal with some of the menu display, the first thought is recursive traversal output, but in the Internet to find a lot of relevant information does not meet their own needs, so I wrote a, if you feel reliable

Please point attention!!! Likes a

First look at some design of database design

The ID of the menu menuId

The name of the menu MenuName

Address URL of the menu

Parent level of the menu

Menu Encoding Menucode

Relatively simple design:

Gossip doesn't say much directly on the code

The entity classes are as follows:

The GET () SET () method is omitted here

Attention:

There's a Childrs set here, because we're going to do recursive traversal to show the child elements of each menu. Select the collection according to your needs I'm using a list if you want the only one to use set.

A list is used here

Next we look at the recursive section as follows:

Just a few lines of code to complete the recursive traversal of all the menus in each menu and will carry their own descendants menu Such a result is you want to do????

Now let's explain what this code means.

Menumapper.conditionlevemenu (Menu.getmenuid ());

The first time you come in, you get the current level element.

Then put the first level element in the child element of my current menu

Because: there's probably a child element underneath my child element, so I'm looping through the elements of the collection above.

for (Menu men:list) {

Then the recursive algorithm recursion (menu) is used to pass in the current menu

}

This method mainly does a few things

1 Query all child elements menu below the current menu

2 If a child element menu is added to the child elements collection of the current menu

We're going to think about what to do if there's a menu element underneath the child element collection.

So the traversal was done

3 looping through a collection of child elements to repeat operations put the menu and enter the method

1 Querying all child elements below the current menu

2 If there are child element menus added to the child elements collection of the current period menu

3 Iterate through the collection of child elements for repeated operations .... Ditto

Until the loop is complete.

The main code is that the most important thing is to implement this feature details to leave you open the brain to think about it!! Hey, what's wrong with this article can be in the following message exchange, if you think this article is helpful to you 推荐 , you can click on the following support me, 关注 Blog has been updated, welcome.

The most reliable Java recursive tree menu display

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.