普通方式效率肯定低。学csdn吧,用xmlhttp。

解决方案 »

  1.   

    我是新手,谈谈我的做法:
    首先将数据从数据库中取出来缓存,在需要构建树的地方从缓存的数据中取出需要的数据构成一个树对象。显示的时候遍历这个树对象就可以了,关于数据库同步问题自己可以加入控制。另外,在生成树对象时,最好用一个属性记录下树的层次关系,比如:第一级是001、002......第二级的就是001001、001002、002001、002002.......依次类推。有一段代码,但不是很通用,感兴趣的可以改写到通用一点。这是构建树对象的。
    public class UserDataModel {  Log log = LogFactory.getLog(UserDataModel.class);
      private GroupService groupService=(GroupService)ContainerManager.getComponent("groupService");
      private UserService userService=(UserService)ContainerManager.getComponent("userService");    public UserDataModel() {
        }    public GroupNode getUserDataModel(int unitId){
          return constrution(unitId);
        }
        public synchronized GroupNode constrution(int unitId){
          if (unitId<0) {
            log.info("avalid unitId: "+unitId);
            return null;
          }
          List groups=groupService.getAllUnitGroup(unitId);
          List users=userService.getUnitUsers(unitId);
          if (groups==null || groups.size()==0) {
            log.info("no group in this unit: "+unitId);
            return null;
          }
          if (users==null || users.size()==0) {
            log.info("no user in this unit: "+unitId);
            return null;
          }
          GroupNode root=getGroupTree(groups);
          root=dealUsers(root,users);
          return root;
        }    private GroupNode getGroupTree(List groups){
          GroupNode root=new GroupNode("G0","用户列表树根");
          root.setPos("000");
          for (Iterator iter = groups.iterator(); iter.hasNext(); ) {
            GroupDto item = (GroupDto)iter.next();
            GroupNode gnode=new GroupNode(item);
            root.addChild(gnode);
          }
          if (root.getChildren().size()<=0) {
            log.info("no groups in this system!");
            return root;
          }
          for (Iterator iter = groups.iterator(); iter.hasNext(); ) {
            GroupDto item = (GroupDto)iter.next();
            String selfKey="G"+String.valueOf(item.getId());
            String key="G"+String.valueOf(item.getParentId());
            GroupNode gnode=(GroupNode)root.getChildren().get(selfKey);
            if (item.getParentId()<1) {
              //root.addChild(gnode);
              continue;
            }
            GroupNode parentNode=getGroupNode(root,key);
            if (parentNode==null) {
              log.info("no parent group to finded, id is :"+key);
              continue;
            }
            root.getChildren().remove(selfKey);
            parentNode.addChild(gnode);
          }
          return root;
        }    private GroupNode getGroupNode(GroupNode root,String key){
          if (root==null || key ==null ){
            log.info("root is null or key is null!");
            return null;
          }
          Map groups=root.getChildren();
          GroupNode reNode=null;
          if (groups.get(key)!=null )
            return (GroupNode)groups.get(key);
          else {
            for (Iterator iter = groups.values().iterator(); iter.hasNext(); ) {
              BaseNode bNode = (BaseNode) iter.next();
              if (bNode instanceof GroupNode) {
                GroupNode item = (GroupNode)bNode;
                reNode = getGroupNode(item, key);
                if (reNode != null)
                  return reNode;
              }
            }
          }
          return null;
        }    private GroupNode dealUsers(GroupNode root,List users){
          if (root==null || users==null ){
            log.info("no group or user ");
          }
          for (Iterator iter = users.iterator(); iter.hasNext(); ) {
            UserDto item = (UserDto)iter.next();
            if (item.getUserType()!=2) continue;
            UserNode unode=new UserNode(item);
            int groupId=item.getGroupId();
            if (groupId<1) {
              root.addChild(unode);
              continue;
            }
            String key="G"+String.valueOf(item.getGroupId());
            GroupNode gnode=getGroupNode(root,key);
            if (gnode==null) {
              log.info("no groupnode for this id: "+key);
              continue;
            }
            gnode.addChild(unode);
          }
          return root;
        }
    有了这个对象,遍历一下就可以显示出树来了。