欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Freemarker如何生成树形导航菜单(递归)

程序员文章站 2024-02-29 12:53:16
很多导航菜单是树形的,即一级一级往下分,这样的结构固然需要用递归来处理。 对于freemarker 来说,宏就相当于函数,其定义了签名及参数列表。 所以在f...

很多导航菜单是树形的,即一级一级往下分,这样的结构固然需要用递归来处理。

对于freemarker 来说,宏就相当于函数,其定义了签名及参数列表。

所以在freemarker页面应这样写:

<#macro menutree menus> 
  <#if menus?? && menus?size gt 0> 
   <#list menus as menu> 
    <tr> 
     <td> 
      <input type="checkbox" name="ids" value="${menu.id}" /> 
     </td> 
     <td> 
   <span title="${menu.name!}" style="margin-left: ${menu.leaf * 30}px;[#if menu.leaf == 0] color: #000000;[/#if]"> 
   ${menu.menuname!} 
   </span> 
     </td> 
     <td> 
     ${menu.url!} 
     </td> 
     <td> 
     ${menu.permissiontext!} 
     </td> 
     <td> 
     ${menu.sortno!} 
     </td> 
     <td> 
      <a href="edit.jhtml?id=${menu.id}">[编辑]</a> 
     </td> 
    </tr> 
   <#if menu.menubeans?? && menu.menubeans?size gt 0> 
    <@menutree menus = menu.menubeans/> 
   </#if> 
   </#list> 
  </#if> 
 </#macro> 
 <!-- 调用宏 生成递归树 --> 
<@menutree menus = dto /> 

后台数据:

[ 
 { 
  "id": 1, 
  "enable": true, 
  "remark": null, 
  "createby": 1, 
  "createtime": 1503546411000, 
  "updateby": 1, 
  "updatetime": 1503546415000, 
  "keyword": null, 
  "menuname": "系统管理", 
  "menutype": 1, 
  "parentid": null, 
  "iconcls": null, 
  "url": null, 
  "expand": null, 
  "sortno": null, 
  "permission": null, 
  "moduleid": 1, 
  "parentname": null, 
  "leaf": 0, 
  "typename": null, 
  "permissiontext": null, 
  "menubeans": [ 
   { 
    "id": 7, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "模块管理", 
    "menutype": 1, 
    "parentid": 1, 
    "iconcls": null, 
    "url": "/module/list", 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 3, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "菜单管理", 
    "menutype": 1, 
    "parentid": 1, 
    "iconcls": null, 
    "url": "/menu/list", 
    "expand": null, 
    "sortno": null, 
    "permission": "admin:add", 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   } 
  ] 
 }, 
 { 
  "id": 2, 
  "enable": true, 
  "remark": null, 
  "createby": 1, 
  "createtime": 1503546411000, 
  "updateby": 1, 
  "updatetime": 1503546415000, 
  "keyword": null, 
  "menuname": "用户管理", 
  "menutype": 1, 
  "parentid": null, 
  "iconcls": null, 
  "url": null, 
  "expand": null, 
  "sortno": null, 
  "permission": null, 
  "moduleid": 1, 
  "parentname": null, 
  "leaf": 0, 
  "typename": null, 
  "permissiontext": null, 
  "menubeans": [ 
   { 
    "id": 9, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "供应商管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 8, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "部门管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 6, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "权限管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": "/user/list-6", 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 5, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "角色管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": "/user/list-5", 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 4, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "分销商管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": "/user/add-4", 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   }, 
   { 
    "id": 10, 
    "enable": true, 
    "remark": null, 
    "createby": 1, 
    "createtime": 1503546411000, 
    "updateby": 1, 
    "updatetime": 1503546415000, 
    "keyword": null, 
    "menuname": "员工管理", 
    "menutype": 1, 
    "parentid": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortno": null, 
    "permission": null, 
    "moduleid": 1, 
    "parentname": null, 
    "leaf": 1, 
    "typename": null, 
    "permissiontext": null, 
    "menubeans": null 
   } 
  ] 
 } 
] 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。