Scenario:
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.
Implementation:
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 -->
Summary:
Use C: import to implement recursion. The exit condition is that no children exists in the list!