访问远程Java对象
19.2.1 服务器端:实现POJO Java类
使用POJO类作为远程Java对象
19.2.2 服务器端:部署POJO Java类
19.2.3 服务器端:配置remote-config.xml
原始remote-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="shoppingCart">
<properties>
<source>com.mark.pojo.shoppingCart</source>
<scope>session</scope>
</properties>
</destination>
</service>
限制远程Java对象可访问方法
<destination id="shoppingCart">
<properties>
<source>com.mark.pojo.shoppingCart</source>
<scope>session</scope>
<include-methods>
<method name="purchaseItem"/>
<method name="deleteItem"/>
</include-methods>
</properties>
</destination>
<destination id="shoppingCart">
<properties>
<source>com.mark.pojo.shoppingCart</source>
<scope>session</scope>
<exclude-methods>
<method name="purchaseItem"/>
</exclude-methods>
</properties>
</destination>
19.2.4 客户端:实例化RemoteObject组件对象
var remoteObj:RemoteObject = new RemoteObject();
remoteObj.destination = "shoppingCart";
19.2.5 客户端:调用远程Java对象方法
<mx:RemoteObject id="remoteObj" destination="shoppingCart"/>
<mx:Button label="购物" click="remoteObj.purchase(‘TV’)"/>
19.2.6 客户端:处理响应和异常
……
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//RemoteObject对象result事件侦听器方法
private function roRespondHandler(event:ResultEvent):void{
//处理远程服务响应
}
//RemoteObject对象fault事件侦听器方法
private function roErrorHandler(event:FaultEvent):void{
//处理远程服务调用异常
}
]]>
</mx:Script>
<!--实例化远程对象组件-->
<mx:RemoteObject id="remoteObj" destination="shoppingCart"
result="roRespondHandler(event)" fault="roErrorHandler(event)"/>
<!--调用远程对象组件-->
<mx:Button label="购物" click="remoteObj.purchase()"/>
……
19.2.7 使用<mx:operation>处理多个方法
使用mx.rpc.remoting.Operation类
使用<mx:method>标签
<mx:RemoteObject id="remoteObj" destination="shoppingCart"
fault="roErrorHandler(event)">
<mx:method name="clearCart" result="clearROResultHandler(event);"/>
<mx:method name="deleteItem"
result="deleteROResultHandler(event);">
<mx:arguments>
<item>{comboItem.selectedItem.data}</item>
</mx:arguments>
</mx:method>
<!-—参数顺序必须与purchaseItem(String itemName,int quantity)方法相匹配-->
<mx:method name="purchaseItem"
result="purchaseROResultHandler(event);">
<mx:arguments>
<item>{comboItem.selectedItem.data}</item>
<qty>{txtQuantity.text}</qty>
</mx:arguments>
</mx:method>
</mx:RemoteObject>
<mx:Button label="购物" click="remoteObj.purchaseItem.send()"/>