Flex tree基于数据库的数据源

最近在研究flex,关于flex tree基于数据库数据的网上的例子基本没有,大部分都是基于xml的对xml的操作实现tree的改变,通过改变数据库数据实现tree的改变例子没有找到,所以分享给大家一个例子:

  我是用hessian实现flex端与java端通讯的

  1.flex端代码

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
        layout
="absolute" 
        verticalAlign
="middle" 
        backgroundColor
="white" creationComplete="init();"> 
  
<mx:Script> 
    
<![CDATA[ 
    
import hessian.service.FlexTestService; 
    
import mx.collections.ICollectionView; 
    
import mx.collections.XMLListCollection; 
    
import mx.controls.Alert; 
    
import mx.controls.Tree; 
    
import mx.events.ListEvent; 
    
import mx.rpc.events.ResultEvent; 
    
import mx.managers.PopUpManager; 
 
    var noteId:String; 
 
    
//初始化从数据库中获得tree 
    private function init():void { 
      getTree(); 
    }
 
    
//监听tree item的click事件 
    private function tree_itemClick(evt:ListEvent):void { 
      var item:Object 
= Tree(evt.currentTarget).selectedItem; 
      
if (tree.dataDescriptor.isBranch(item)) { 
        tree.expandItem(item, 
!tree.isItemOpen(item), true); 
      }
 
    }
 
    
//tree的item有子节点的显示(子节点个数) 
    private function tree_labelFunc(item:XML):String { 
      var children:ICollectionView; 
      var suffix:String 
= ""; 
      
if (tree.dataDescriptor.isBranch(item)) { 
        children 
= tree.dataDescriptor.getChildren(item); 
        suffix 
= " (" + children.length + ")"; 
      }
 
      
return item[tree.labelField] + suffix; 
    }
 
 
    [Bindable] 
    
public var selectedNode:XML; 
    
//当tree发生change事件时的事件 
    public function treeChanged(event:Event):void { 
      
//将选中的节点转换成xml,赋予selectedNode,as为转换类型 
      selectedNode = Tree(event.target).selectedItem as XML; 
      noteId 
= selectedNode.@sid; 
    }
 
    
//获得tree的方法 
    public function getTree():void { 
      
//构造service时指定回调方法名称 
      var service:FlexTestService = new FlexTestService(result); 
      service.getTree(); 
    }
 
 
    var returnString:String; 
    var treeDate:XML 
 
    
//回调方法 
    public function result(data:Object):void { 
      
//java端返回的数据转化成String类型 
      returnString = data as String; 
      
//利用returnString构造一个xml 
      treeDate = new XML(returnString); 
      
//把构造的xml文件绑定到tree上 
      tree.dataProvider = treeDate; 
    }
 
    
//删除某一节点 
    public function delNote():void { 
      var service:FlexTestService 
= new FlexTestService(result); 
      service.delNote(noteId); 
    }
 
    
//增加一个节点 
    public function addNote():void { 
      var service:FlexTestService 
= new FlexTestService(result); 
      service.addNote(noteId, nodeName.text); 
    }
 
    
//重命名节点 
    public function updateNote():void { 
      var service:FlexTestService 
= new FlexTestService(result); 
      service.updateNote(noteId, nodeName.text); 
    }
 
    ]]
> 
  
</mx:Script> 
 
  
<mx:Tree id="tree" 
       showRoot
="false" 
       labelField
="@label" 
       labelFunction
="tree_labelFunc" 
       width
="258" 
       rowCount
="6" 
       itemClick
="tree_itemClick(event);" x="301" y="205" height="208" change="treeChanged(event);"/> 
  
<mx:Button width="98" height="31" click="addNote();" x="374" y="440" label="添加" fontSize="16"/> 
  
<mx:Button width="98" height="31" click="updateNote();" x="374" y="482" label="修改" fontSize="16"/> 
  
<mx:Button width="98" height="31" click="delNote();" x="374" y="521" label="删除" fontSize="16"/> 
  
<mx:TextArea text="您选择的节点: {selectedNode.@label}" x="576" y="207" height="206" width="261"/> 
  
<mx:Text x="576" y="482" text="节点名称" fontSize="14" fontWeight="bold"/> 
  
<mx:TextInput id="nodeName" x="660" y="479" fontSize="16"/> 
</mx:Application> 



2. flex 端 FlexTestService.as
package hessian.service { 
import mx.rpc.AsyncToken; 
import mx.controls.Alert; 
 
import hessian.client.HessianService; 
 
 
public class FlexTestService extends BaseService { 
 
  
private static var _userService : HessianService; 
 
  
//调用java端服务,调用的方法名要与java端服务接口名一致 
  protected static function getFlexTestService():HessianService { 
    
if (!_userService) { 
      _userService 
= new HessianService("http://localhost/remoting/testService"); 
    }
 
    
return _userService; 
  }
 
 
  
public function FlexTestService(resultCallback:Function = null, faultCallback:Function = null) { 
    
super(resultCallback, faultCallback); 
  }
 
 
  
//获得tree数据 
  public function getTree():void { 
    var token:AsyncToken 
= getFlexTestService().getTree.send(); 
    token.addResponder(
this); 
  }
 
  
//增加节点 
  public function addNote(sid:String, name:String):void { 
    var token:AsyncToken 
= getFlexTestService().addNode.send(sid, name); 
    token.addResponder(
this); 
  }
 
  
//删除节点 
  public function delNote(sid:String):void { 
    var token:AsyncToken 
= getFlexTestService().delNode.send(sid); 
    token.addResponder(
this); 
  }
 
  
//更新节点 
  public function updateNote(sid:String, name:String):void { 
    var token:AsyncToken 
= getFlexTestService().updateNode.send(sid, name); 
    token.addResponder(
this); 
  }
 
 }
 
}
 



3.java端服务接口  TestService.java
package net.shopin.api; 
 
import net.shopin.view.CatalogVO; 
 
import java.util.List; 
 
public interface TestService { 
  
/** 
   * 获取所有节点 
   * 
@return 
   
*/
 
  String getTree(); 
  
/** 
   * 增加节点 
   * 
@return 
   
*/
 
  String addNode(String sid,String name); 
  
/** 
   * 删除节点 
   * 
@return 
   
*/
 
  String delNode(String sid); 
  
/** 
   * 更新节点 
   * 
@return 
   
*/
 
  String updateNode(String sid,String name); 
}
 



4.java 端服务接口实现 TestServiceImpl
package net.shopin.business; 
 
import com.shopin.modules.orm.hibernate.SimpleHibernateDao; 
import com.shopin.modules.utils.DozerMapper; 
import java.util.List; 
import java.util.ArrayList; 
import net.shopin.entity.Catalog; 
import net.shopin.entity.ProductList; 
import net.shopin.api.TestService; 
import net.shopin.view.CatalogVO; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.stereotype.Component; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.hibernate.SessionFactory; 
import org.hibernate.Criteria; 
import org.hibernate.criterion.Restrictions; 
import org.apache.commons.lang.ObjectUtils; 
 
@Transactional 
@Component(
"testService") 
public class TestServiceImpl implements TestService { 
  
private SimpleHibernateDao<Catalog, Long> testDao; 
  @Autowired 
  
public void setSessionFactory(SessionFactory sessionFactory) { 
    testDao 
= new SimpleHibernateDao<Catalog, Long>(sessionFactory, Catalog.class); 
  }
 
 
  @Transactional(readOnly 
= true) 
  
public String getTree() { 
    Catalog catalog 
= testDao.findByUnique("sid", Long.parseLong("1")); 
    Criteria criteria 
= testDao.createCriteria(); 
    criteria.add(Restrictions.eq(
"fatherNodeId", catalog)); 
    List
<Catalog> list = criteria.list(); 
    String tree 
= listToxml(list); 
    
return tree; 
  }
 
 
  
private List<Catalog> getSonNode(String sid) { 
//    System.out.println("--------getSonNode-----------"); 
    String hql = "from Catalog as o where o.fatherNodeId = ? "; 
    Catalog catalog 
= testDao.findByUnique("sid", Long.parseLong(sid)); 
    List
<Catalog> list = testDao.find(hql, catalog); 
    
return list; 
  }
 
 
  
//把list对象转化成XML 
  private String listToxml(List flist) { 
    ArrayList list 
= (ArrayList) flist; 
    String treedata 
= "<treeRoot>\n"; 
    treedata 
= CreateXmlNode(list, 1L, treedata); 
    treedata 
= treedata + "</treeRoot>\n"; 
//    System.out.println(treedata); 
    return treedata; 
  }
 
 
  
/** 
   * 递归解析list –> Xml 
   
*/
 
  
private String CreateXmlNode(List<Catalog> list, Long nodepid, String cratedata) { 
    
for (int a = 0; a < list.size(); a++) { 
      Catalog fsort 
= (Catalog) list.get(a); 
      
if ((fsort.getFatherNodeId().getSid().toString()).equals(nodepid.toString())) { 
        List
<Catalog> lists = getSonNode(fsort.getSid().toString()); 
        
if (lists.size() > 0) { 
          cratedata 
= cratedata + "<second label=\"" + fsort.getFatherNodeId().getNodeName() + "-" + fsort.getNodeName() + "\" sid=\"" + fsort.getSid() + "\">\n"; 
          cratedata 
= CreateXmlNode(lists, fsort.getSid(), cratedata); 
          cratedata 
= cratedata + "</second>\n"; 
        }
 else { 
          cratedata 
= cratedata + "<third label=\"" + fsort.getFatherNodeId().getNodeName() + "-" + fsort.getNodeName() + "\" sid=\"" + fsort.getSid() + "\"/>\n"; 
        }
 
      }
 
    }
 
    
return cratedata; 
  }
 
 
  
public String addNode(String sid, String name) { 
    Catalog catalog 
= testDao.findByUnique("sid", Long.parseLong(sid)); 
    Catalog newCatalog 
= new Catalog(); 
    newCatalog.setNodeName(name); 
    newCatalog.setFatherNodeId(catalog); 
    testDao.save(newCatalog); 
    String data 
= getTree(); 
    
return data; 
  }
 
 
  
public String delNode(String sid) { 
    
if (getSonNode(sid).size() > 0) { 
      System.out.println(
"不可以删除此节点"); 
    }
 else { 
      String hql 
= "from Catalog as o where o.sid = ? "; 
      Catalog catalog 
= testDao.findByUnique("sid", Long.parseLong(sid)); 
      testDao.delete(catalog); 
    }
 
    String data 
= getTree(); 
    
return data; 
  }
 
 
  
public String updateNode(String sid, String name) { 
    String hql 
= "from Catalog as o where o.sid = ? "; 
    Catalog catalog 
= testDao.findByUnique("sid", Long.parseLong(sid)); 
    catalog.setNodeName(name); 
    testDao.save(catalog); 
    String data 
= getTree(); 
    
return data; 
  }
 
}
 

posted on 2010-10-29 13:48 aiaiwoo 阅读(737) 评论(1)  编辑  收藏 所属分类: AC3/FLEX

评论

# re: Flex tree基于数据库的数据源 2011-06-21 17:55 Jack Yang

请问有没有Flex版的迭代树,你这个其中的1L我不懂怎么来的。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

导航

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜