普通方式效率肯定低。学csdn吧,用xmlhttp。
解决方案 »
- JFileChooser.showSaveDialog()设置默认文件名
- 关于字符串的equals()方法
- 求学习方法~~三大框架都学一个月了还没有头绪~~~~自信被毁了,想退出
- JTextField里设一个回车触发事件,是不是KeyListener的KeyPressed()方法??
- 字符串分割问题
- 读取中文的问题,文件夹是中文,结果读不出正确的路径
- java中如何控制一个方法的最多执行时间?
- 服务器需不需要用线程池技术?
- system property是存放在哪里的?
- 请问:在一个Frame里显示一个JInternalFrame,想把JInternalFrame位置固定到Frame的右上?
- 请问在jBuilder x中怎么样把某一行变成红色
- 谁有java的帮助文档chm格式的
首先将数据从数据库中取出来缓存,在需要构建树的地方从缓存的数据中取出需要的数据构成一个树对象。显示的时候遍历这个树对象就可以了,关于数据库同步问题自己可以加入控制。另外,在生成树对象时,最好用一个属性记录下树的层次关系,比如:第一级是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;
}
有了这个对象,遍历一下就可以显示出树来了。