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

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

对于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 
   } 
  ] 
 } 
] 

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。