Jstl implements recursive display of tree structure data!

Source: Internet
Author: User


Data with a tree structure is stored in the database

Ing to object tree (ID, name, URL...), tree adds a self-described attribute list <tree> Children

Recursive tree structure from the database based on the root node is put into the list <tree> treelist. It is now required to display in the display by hierarchy:

Problem: the level of the unknown tree does not support function recursion in jstl.

Solution: the solution is implemented in the form of file inclusion, although not elegant.

Knowledge preparation:

Jstl C label usage, C: foreach, C: Import

JSP scope: Page, request, session, Application

C: The import page can share data at the request scope level or above, but cannot share data at the page scope level.


Home PageCode:

<Table> <C: Set Var = "Index" value = "0" Scope = "request"/> <! -- Auto-increment number. Note scope --> <C: Set Var = "level" value = "0" Scope = "request"/> <! -- Record tree hierarchy. Note scope --> <C: Import url = "_ R. jsp"/> </table>

included page :__ R. JSP code:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ include file ="/commons/pages/taglibs. JSP "%> <! -- Introduce the jstl package --> <C: foreach Var = "cur" items = "$ {treelist}" varstatus = "vs"> <C: set Var = "Index" value = "$ {index + 1}" Scope = "request"/> <! -- Every cycle, index + 1 --> <tr> <TD align = "center" nowrap = "nowrap" >$ {index} </TD> <TD align = "center" nowrap = "nowrap ">$ {level} </TD> <TD align =" center "nowrap =" nowrap ">$ {cur. name} </TD> </tr> <C: If test = "$ {fn: length (cur. children)> 0} "> <! -- If childen --> <C: Set Var = "level" value = "$ {level + 1}" Scope = "request"/> <! -- Cyclically sublist, level + 1 --> <C: Set Var = "treelist" value = "$ {cur. Children}" Scope = "request"/> <! -- Note that the sub-list overwrites treelist in the request scope --> <C: Import url = "_ R. jsp"/> <! -- This is Recursion --> </C: If> </C: foreach> <C: set Var = "level" value = "$ {level-1}" Scope = "request"/> <! -- When exiting, level-1 -->

Use C: import to implement recursion. The exit condition is that no children exists in the list!

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.