最近在看Flex,用RemoteObject方法(Blazeds)做了个动态加载树的小例子,不是很好,以后再慢慢改进吧.
mxml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script >
<![CDATA[
import mx.events.ListEvent;
import mx.events.TreeEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
private var _node:XML;
[Bindable]
private var xmlTree:XML = <node nodename="root" id="0" is_parent="1" parentnode="" action="">
<nodetemp nodename="" id="" is_parent="" parentnode="" action=""></nodetemp>
</node>;
private function addNewNode(treenode:XML,resultList:ArrayCollection):void{
if(null==resultList){
return ;
}
for(var i:Number=0;i<resultList.length;i++){
var newNode:XML = <node/>;
newNode.@id=resultList[i]["node"];
newNode.@is_parent=resultList[i]["is_parent"];
newNode.@nodename=resultList[i]["nodename"];
newNode.@parentnode=resultList[i]["parentnode"];
newNode.@action=resultList[i]["action"];
right_tree.dataDescriptor.addChildAt(treenode, newNode, i, treenode);
//如果有子节点就创建临时节点,让这个节点有itemOpen的方法
if("1"==resultList[i]["is_parent"]){
var nodetemp:XML = <nodetemp/>;
right_tree.dataDescriptor.addChildAt(newNode, nodetemp, 0, newNode);
}
}
}
private function getRightByParntResult(event:ResultEvent):void{
var resultList:ArrayCollection =event.result as ArrayCollection;
addNewNode(_node,resultList);
}
private function tree_itemOpen(evt:TreeEvent):void {
var t:Tree = evt.currentTarget as Tree;
_node= evt.itemRenderer.data as XML;
var hasload:Boolean = false;
var xmllist:XMLList = _node.elements("nodetemp");
//删除拥有子节点的临时节点
if(null != xmllist && xmllist.length() >0 ){
for(var i:Number=0;i<xmllist.length();i++){
var temp:XML = xmllist[i] as XML
right_tree.dataDescriptor.removeChildAt(_node,temp,i,_node);
}
}
xmllist =_node.elements("node");
//如果这个节点已经取过子节点就返回,不需要再取
if(null!=xmllist&&xmllist.length()>0){
return;
}
rightHandler.endpoint="messagebroker/amf";
var nodeid:String=_node.@id ;
rightHandler.getRightByParent(nodeid);
}
]]>
</mx:Script>
<mx:RemoteObject id="rightHandler" destination="rightHandler" >
<mx:method name="getRightByParent" result="getRightByParntResult(event)" />
</mx:RemoteObject>
<mx:Tree id="right_tree"
dataProvider="{xmlTree}"
itemOpen="tree_itemOpen(event)"
labelField="@nodename"
bottom="0" top="25" right="0" left="0">
</mx:Tree>
</mx:Application>
Java代码
package com.cpk.service;
import java.util.List;
import java.util.Map;
import com.cpk.dao.RightDao;
public class RightHandler {
public List<Map<String,String>> getRightByParent(String parentID){
RightDao dao=new RightDao();
return dao.query(parentID);
}
}
package com.cpk.dao;
import java.util.List;
import java.util.Map;
import com.caipk.newjdbc.sql.Finder;
import com.caipk.newjdbc.template.Jdbc;
public class RightDao extends Jdbc {
public List<Map<String,String>> query(String parentid){
return this.query(
Finder.create("select node,parentnode,nodename,action,title,closeimg,openimg,")
.append(" decode( (select node from right t where node in(select distinct parentnode from right where status='0') and t.node=right.node and parentnode=?),null,'0','1') as is_parent ")
.append(" from right")
.append(" where parentnode=?")
.setParam(parentid)
.setParam(parentid)
);
}
}
remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<!--
<destination id="GenericDestination">
<properties>
<source>*</source>
</properties>
</destination>
-->
<destination id="EmployeeHandler">
<properties>
<source>com.cpk.service.EmployeeHandler</source>
</properties>
</destination>
<destination id="rightHandler">
<properties>
<source>com.cpk.service.RightHandler</source>
</properties>
</destination>
</service>