下面例子是利用lcds调用java类,实现和java的通信,要注意几个小细节:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script >
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var helloResult:String;
private function sayHelloTo():void {
ro.sayHelloTo(inputText.text);
}
private function resultHandler(event:ResultEvent):void {
inputText.text="";
helloResult = event.result as String;
}
]]>
</mx:Script >
<mx:RemoteObject id = "ro" endpoint="/TestFlex/messagebroker/amf" destination="helloworld" result = "resultHandler(event)"/>
<mx:HBox width = "100%" >
<mx:TextInput id = "inputText" />
<mx:Button label = "Submit" click = "sayHelloTo();" />
</mx:HBox >
<mx:Label text = "{helloResult}" />
</mx:Application>
这个是网上出现最多的一个例子,如果复制过来会出错的,其中一个原因就是要根据你的环境设置endpoint,格式:http://{server.name}:{server.port}/{context.root}/messagebroker/amf”
在Flex文件夹下remoting-config.xml文件里要配置你的java类:
<destination id="helloworld">
<properties>
<source>HelloWorld</source>
</properties>
</destination>
public class HelloWorld {
public String sayHelloTo(String name){
System.out.print("hello");
return "hello"+name;
}
}
还有一个要注意的是,修改remoting-config.xml后要重启服务器,不然会出现问题的。