Freemarker 树形导航菜单(递归)

很多导航菜单是树形的,即一级一级往下细分,这样的结构固然需要用递归来处理。 
对于Freemarker 来说,宏就相当于函数,其定义了签名及参数列表。 
所以在页面应这样写:

    <ul>
      
      <#macro bpTree children>
        <#if children?? && children?size gt 0>
       <#list children as child>
        <#if child.children?? && child.children?size gt 0>
        <li class="treeview">
          <a href="javascript:void(0)">
            <i class="${(child.mIcon=='')?string('fa fa-share',child.mIcon) }" aria-hidden="true">i> <span>${child.mCname}span>
            <i class="fa fa-angle-left pull-right" aria-hidden="true">i>
          a>
          <ul class="treeview-menu">
            <@bpTree children=child.children />
          ul>
        li>
        <#else>
            <li><a href="javascript:void(0)" onclick="loadContent('${child.mUrl}')"><i class="${ (child.mIcon=='')?string('fa fa-circle-o',child.mIcon) }" aria-hidden="true">i> ${child.mCname}a>li>   
        #if>
       #list>
    #if>
    #macro>
       
       <@bpTree children=treeMenu />
  ul>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

treeMenu是Controller返回的JSON中的根元素,其值是JSONArray,所以最关键的核心问题是如何生成递归地数据结构。叶子节点的children为空数组。 
Fremarker 宏 请参考博文:Freemarker定义宏的用法

后台Service查询数据库处理数据生成JSONArray的数据结构格式:

[
    {
        "mCname": "框架应用管理",
        "mIcon": "fa fa-square-o",
        "mAppid": 1,
        "mIsclose": 0,
        "mIssys": 1,
        "mUrl": "",
        "children": [
            {
                "mCname": "模块管理",
                "mIcon": "fa fa-cube",
                "mAppid": 1,
                "mIsclose": 0,
                "mIssys": 1,
                "mUrl": "AppMgr/ModuleManager/default.aspx",
                "children": [],
                "mOrderlevel": "0001",
                "mParentName": "框架应用管理",
                "mParentid": 1,
                "moduleid": 2,
                "mModulecode": "S00M00",
                "aAppName": "框架应用管理"
            },
            {
                "mCname": "配置管理",
                "mIcon": "fa fa-cog",
                "mAppid": 1,
                "mIsclose": 0,
                "mIssys": 1,
                "mUrl": "AppMgr/ConfigManager/default.aspx",
                "children": [],
                "mOrderlevel": "0002",
                "mParentName": "框架应用管理",
                "mParentid": 1,
                "moduleid": 3,
                "mModulecode": "S00M01",
                "aAppName": "框架应用管理"
            },
            {
                "mCname": "应用字段设置",
                "mIcon": "fa fa-key",
                "mAppid": 1,
                "mIsclose": 0,
                "mIssys": 1,
                "mUrl": "AppMgr/FieldManager/default.aspx",
                "children": [],
                "mOrderlevel": "0003",
                "mParentName": "框架应用管理",
                "mParentid": 1,
                "moduleid": 4,
                "mModulecode": "S00M02",
                "aAppName": "框架应用管理"
            },
            {
                "mCname": "系统异常日志",
                "mIcon": "fa fa-file-pdf-o",
                "mAppid": 1,
                "mIsclose": 0,
                "mIssys": 1,
                "mUrl": "AppMgr/SystemErrorLog/default.aspx",
                "children": [],
                "mOrderlevel": "0004",
                "mParentName": "框架应用管理",
                "mParentid": 1,
                "moduleid": 5,
                "mModulecode": "S00M03",
                "aAppName": "框架应用管理"
            },
            {
                "mCname": "事件日志",
                "mIcon": "fa fa-file-zip-o",
                "mAppid": 1,
                "mIsclose": 0,
                "mIssys": 1,
                "mUrl": "AppMgr/EventLog/default.aspx",
                "children": [],
                "mOrderlevel": "0005",
                "mParentName": "框架应用管理",
                "mParentid": 1,
                "moduleid": 6,
                "mModulecode": "S00M04",
                "aAppName": "框架应用管理"
            }
        ],
        "mOrderlevel": "0000",
        "mParentid": 0,
        "moduleid": 1,
        "mModulecode": "S00",
        "aAppName": "框架应用管理"
    },
    {
        "mCname": "系统管理",
        "mIcon": "fa fa-cogs",
        "mAppid": 2,
        "mIsclose": 0,
        "mIssys": 0,
        "mUrl": "",
        "children": [
            {
                "mCname": "行政区管理",
                "mIcon": "fa fa-circle",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/AreaManager/default.aspx",
                "children": [],
                "mOrderlevel": "0001",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 8,
                "mModulecode": "S10M01",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "组织机构管理",
                "mIcon": "fa fa-tree",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/CompanyManager/default.aspx",
                "children": [],
                "mOrderlevel": "0002",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 9,
                "mModulecode": "S10M02",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "角色权限管理",
                "mIcon": "fa fa-user-secret",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/RoleManager/default.aspx",
                "children": [],
                "mOrderlevel": "0003",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 10,
                "mModulecode": "S10M03",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "用户管理",
                "mIcon": "fa fa-users",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/UserManager/default.aspx",
                "children": [],
                "mOrderlevel": "0004",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 11,
                "mModulecode": "S10M04",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "数据字典",
                "mIcon": "fa fa-leaf",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/FieldValueManager/default.aspx",
                "children": [],
                "mOrderlevel": "0005",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 12,
                "mModulecode": "S10M05",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "在线用户列表",
                "mIcon": "fa fa-user",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "SysMgr/OnlineUser/default.aspx",
                "children": [],
                "mOrderlevel": "0009",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 16,
                "mModulecode": "S10M09",
                "aAppName": "应用系统管理"
            },
            {
                "mCname": "系统设置",
                "mIcon": "fa fa-cog",
                "mAppid": 2,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "",
                "children": [
                    {
                        "mCname": "事件日志查询",
                        "mIcon": "fa fa-search-minus",
                        "mAppid": 2,
                        "mIsclose": 0,
                        "mIssys": 0,
                        "mUrl": "SysMgr/EventManager/default.aspx",
                        "children": [],
                        "mOrderlevel": "0006",
                        "mParentName": "系统设置",
                        "mParentid": 43,
                        "moduleid": 13,
                        "mModulecode": "S10M06",
                        "aAppName": "应用系统管理"
                    },
                    {
                        "mCname": "系统参数管理",
                        "mIcon": "fa fa-square",
                        "mAppid": 2,
                        "mIsclose": 0,
                        "mIssys": 0,
                        "mUrl": "SysMgr/SystemParam/default.aspx",
                        "children": [],
                        "mOrderlevel": "0007",
                        "mParentName": "系统设置",
                        "mParentid": 43,
                        "moduleid": 14,
                        "mModulecode": "S10M07",
                        "aAppName": "应用系统管理"
                    },
                    {
                        "mCname": "系统维护",
                        "mIcon": "fa fa-shield",
                        "mAppid": 2,
                        "mIsclose": 0,
                        "mIssys": 0,
                        "mUrl": "SysMgr/SystemState/default.aspx",
                        "children": [],
                        "mOrderlevel": "0008",
                        "mParentName": "系统设置",
                        "mParentid": 43,
                        "moduleid": 15,
                        "mModulecode": "S10M08",
                        "aAppName": "应用系统管理"
                    }
                ],
                "mOrderlevel": "0010",
                "mParentName": "系统管理",
                "mParentid": 7,
                "moduleid": 43,
                "mModulecode": "S10M10",
                "aAppName": "应用系统管理"
            }
        ],
        "mOrderlevel": "0000",
        "mParentid": 0,
        "moduleid": 7,
        "mModulecode": "S10",
        "aAppName": "应用系统管理"
    },
    {
        "mCname": "查询&报表统计",
        "mIcon": "",
        "mAppid": 3,
        "mIsclose": 0,
        "mIssys": 0,
        "mUrl": "",
        "children": [
            {
                "mCname": "快递单查询",
                "mIcon": "",
                "mAppid": 3,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "ExpressMgr/ExpDocQuery/default.aspx",
                "children": [],
                "mOrderlevel": "0501",
                "mParentName": "查询&报表统计",
                "mParentid": 22,
                "moduleid": 35,
                "mModulecode": "S36M01",
                "aAppName": "物流快递管理系统"
            },
            {
                "mCname": "快递营收统计",
                "mIcon": "",
                "mAppid": 3,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "ExpressMgr/ExpIncomeStat/default.aspx",
                "children": [],
                "mOrderlevel": "0502",
                "mParentName": "查询&报表统计",
                "mParentid": 22,
                "moduleid": 36,
                "mModulecode": "S36M02",
                "aAppName": "物流快递管理系统"
            },
            {
                "mCname": "快递业务流量分析",
                "mIcon": "",
                "mAppid": 3,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "ExpressMgr/ExpDataAnalysis/default.aspx",
                "children": [],
                "mOrderlevel": "0503",
                "mParentName": "查询&报表统计",
                "mParentid": 22,
                "moduleid": 37,
                "mModulecode": "S36M03",
                "aAppName": "物流快递管理系统"
            },
            {
                "mCname": "快递业务报表",
                "mIcon": "",
                "mAppid": 3,
                "mIsclose": 0,
                "mIssys": 0,
                "mUrl": "ExpressMgr/ExpCustomRpt/default.aspx",
                "children": [],
                "mOrderlevel": "0504",
                "mParentName": "查询&报表统计",
                "mParentid": 22,
                "moduleid": 38,
                "mModulecode": "S36M04",
                "aAppName": "物流快递管理系统"
            }
        ],
        "mOrderlevel": "0500",
        "mParentid": 0,
        "moduleid": 22,
        "mModulecode": "S36",
        "aAppName": "物流快递管理系统"
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333

要生成如上所示的JSONArray,其Service层也需要用递归来处理数据,Service层的写法,

// 返回json格式的树形导航菜单
    public JSONArray getTreeMenu() {
        return treeMenuList(getModules(), 0);
    }

    // 菜单树形结构
    public JSONArray treeMenuList(List menuList, int parentId) {
        JSONArray childMenu = new JSONArray();
        for (FrmModule menu : menuList) {
            if (parentId == menu.getmParentid()) {
                JSONObject jo = (JSONObject) JSONObject.toJSON(menu);
                JSONArray c_node = treeMenuList(menuList, menu.getModuleid());
                jo.put("children", c_node);
                childMenu.add(jo);
            }
        }
        return childMenu;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

然后在Controller层调用Service层生成的JSONArray,封装返回的JSON数据:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model model) {
        JSONArray ja = moduleService.getTreeMenu();
        System.out.println(ja.toJSONString());
        model.addAttribute("treeMenu",ja );
        return "index";
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

最后生成的树形菜单如下所示:

Freemarker 树形导航菜单(递归)_第1张图片

你可能感兴趣的:(Freemarker)