ajax做json拼接

本文档展示了如何在Action中处理Ajax请求,并在业务处理层利用Hibernate查询数据,将查询到的一级和二级菜单转换为JSON格式进行拼接。通过遍历TtMenu对象,创建JSON对象表示菜单结构,包括一级菜单的id和名称,以及包含二级菜单的items属性。每个二级菜单包含id、text和href属性。最后返回拼接后的JSON字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[
    {
        "id": "100000",
        "menu": [
            {
                "text": "部门管理",
                "items": [
                    {
                        "id": "100006",
                        "text": "部门增加",
                        "href": "http://localhost:8080/RBAC/Department/Department_add.jsp"
                    },
                    {
                        "id": "100007",
                        "text": "部门维护",
                        "href": "http://localhost:8080/RBAC/selectDepartment"
                    }
                ]
            }
        ]
    },
    {
        "id": "100001",
        "menu": [
            {
                "text": "用户管理",
                "items": [
                    {
                        "id": "100008",
                        "text": "用户增加",
                        "href": "http://localhost:8080/RBAC/getDeptNameList"
                    },
                    {
                        "id": "100009",
                        "text": "用户维护",
                        "href": "http://localhost:8080/RBAC/selectUser"
                    }
                ]
            }
        ]
    },
    {
        "id": "100002",
        "menu": [
            {
                "text": "角色管理",
                "items": [
                    {
                        "id": "100010",
                        "text": "角色增加",
                        "href": "http://localhost:8080/RBAC/Role/role_add.jsp"
                    },
                    {
                        "id": "100011",
                        "text": "角色维护",
                        "href": "http://localhost:8080/RBAC/selectRole"
                    }
                ]
            }
        ]
    },
    {
        "id": "100003",
        "menu": [
            {
                "text": "菜单管理",
                "items": [
                    {
                        "id": "100012",
                        "text": "菜单增加",
                        "href": "http://localhost:8080/RBAC/selectFatherMenu"
                    },
                    {
                        "id": "100013",
                        "text": "菜单维护",
                        "href": "http://localhost:8080/RBAC/selectMenu"
                    }
                ]
            }
        ]
    },
    {
        "id": "100004",
        "menu": [
            {
                "text": "角色人员管理",
                "items": [
                    {
                        "id": "100014",
                        "text": "角色人员维护",
                        "href": "http://localhost:8080/RBAC/selectRoleUser"
                    }
                ]
            }
        ]
    },
    {
        "id": "100005",
        "menu": [
            {
                "text": "权限管理",
                "items": [
                    {
                        "id": "100015",
                        "text": "用户权限设置",
                        "href": "http://localhost:8080/RBAC/selectUserMenu"
                    },
                    {
                        "id": "100016",
                        "text": "部门权限设置",
                        "href": "http://localhost:8080/RBAC/selectDeptMenu"
                    },
                    {
                        "id": "100017",
                        "text": "角色权限设置",
                        "href": "http://localhost:8080/RBAC/selectRoleMenu"
                    }
                ]
            }
        ]
    }

]

在action

//处理一个ajax请求
    public void child(){
        String st = ser.child();
        
        //异步传值
        HttpServletResponse res=ServletActionContext.getResponse();
        res.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter pw=res.getWriter();
            pw.print(st);
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



对这个文档做拼接在业务处理层

public String child() {
        List<TtMenu> menulist=dao.find("from TtMenu where fatherMenuId=1");
        JSONArray all = new JSONArray();
        JSONObject first_menu = new JSONObject();
        
        for(TtMenu mn:menulist){//一级菜单
            
            first_menu.put("id", mn.getMenuId());
        
            //拼一级菜单
            JSONArray first_arr = new JSONArray();
            //只放一个json(一级菜单)对象,包含二级菜单
            
            JSONObject first= new JSONObject();
            first.put("text", mn.getMenuName());
            
            JSONArray second_arr = new JSONArray();
            //拼二级菜单
            JSONObject second= new JSONObject();
            
            for(TtMenu m:mn.getChildMenu()){//二级菜单
                second.put("id",m.getMenuId() );
                second.put("text", m.getMenuName());
                second.put("href", m.getMenuValue());
                
                second_arr.add(second);
            }
            
            first.put("items", second_arr);
            first_arr.add(first);
            first_menu.put("menu", first_arr);
            all.add(first_menu);
        }
        
        return all.toString();

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值