dojo1.1树的两种实现——一次性加载整棵树

/**
* @descrtion: 直接获取树的整个结点, 非懒加载方式, 加上缓存, 根本就不怕它慢. 非懒加载的树主要用于静态的树,例如选择城市等
* @return
* @throws JSONException
* @author hi.baidu.com/javaroad */
public String getTreeRecursive() throws JSONException {
ITreeFactory factory = (ITreeFactory) CrmContexts.getBean(CrmContexts.getRequestParam("treeId"));
// 加上缓存
String key = CrmContexts.getRequestParam("CacheKey");
Object retRoot = CacheUtil.get("TREEROOT", key);

if (retRoot == null) {
JSONArray ret = new JSONArray();
Object[] objPool = new Object[2000]; // 结点对象池2000个够了
Object[] nodes = factory.getRoots();
int index = 0; // 记录结点对象池最后一个元素的位置
if (nodes == null) {
return ret.toString();
}
for (int i = 0; i < nodes.length; i++) {
objPool[index++] = nodes[i];
}

for (int i = 0; i < objPool.length; i++) {
Object node = objPool[i];
if (node == null) {
continue;
}
JSONObject obj = new JSONObject();
String id = factory.getId(node);
obj.put("objectId", id);
if (i < nodes.length) {
obj.put("type", "root");
}
obj.put("title", factory.getTitle(node));
if (!factory.isLeaf(node)) {
// 有子编码
Object[] children = factory.getChildren(node);
if (children != null && children.length > 0) {
// 有孩子节点
JSONArray child = new JSONArray();
for (int j = 0; j < children.length; j++) {
objPool[index++] = children[j];
JSONObject node1 = new JSONObject();
node1.put("_reference", factory.getId(children[j]));
child.put(j, node1);
}
obj.put("children", child);
}
}
ret.put(obj);
}
retRoot = ret;
CacheUtil.put("TREEROOT", key, retRoot);
}

String retu = "{identifier: 'objectId', label: 'title', items: " + retRoot.toString() + "}";
return retu;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值