準(zhǔn)備:
JDK:sun jdk 1.6
開(kāi)發(fā)工具:eclipse+flex builder
flex sdk版本: 3.2
應(yīng)用服務(wù)器: tomcat 6.0
blazed:blazeds-turnkey-3.2.0.3978 下載地址是:http://flexorg.wip3.adobe.com/blazeds/3.0.x/milestone/3978/blazeds-turnkey-3.2.0.3978.zip
新建項(xiàng)目:
等全部環(huán)境安裝好后,打開(kāi)裝了flex builder(flex 4.0以后叫flash builder了),選擇Flex Development 視圖,新建項(xiàng)目選擇Flex Project ,填寫(xiě)新建項(xiàng)目的名稱,項(xiàng)目存儲(chǔ)的目錄,在Server Technology標(biāo)簽中的application Server Type選擇J2EE,選中Use remote object access service和Create combined Java/Flex project using WTP(這個(gè)一定要記的選上)選項(xiàng),然后Next.如下圖:

在第二頁(yè)中J2EE settings,Target runtime如果還沒(méi)有可以先添加一個(gè)預(yù)先準(zhǔn)備后的tomcat環(huán)境。然后選擇它。在Compiled Flex application location的Out Folder改成與上面Content Folder相同的WebContent,注意這個(gè)一定要改,否則無(wú)法自動(dòng)編譯過(guò)去,調(diào)試時(shí)瀏覽器無(wú)法訪問(wèn)。如下圖:

新建好項(xiàng)目后,還要修改一個(gè)關(guān)鍵的項(xiàng)目屬性。將Flex Server標(biāo)簽中的Context Root修改為FirstJavaFlex(這一塊很重要,在沒(méi)有配置RemoteObject 的endPoint屬性的時(shí)候,會(huì)用這個(gè)Context去編譯Flex),還要注意一下Root URL參數(shù)中的端口號(hào),因?yàn)槟J(rèn)是8080,可是如果你用的是blazeds-turnkey包中的tomcat的時(shí)候,那個(gè)端口號(hào)是8400記著一定也要改一下。如下圖:

就這樣新建好項(xiàng)目之后,java源文件放在src目錄下,flex源文件放在flex_src目錄下。
接下來(lái)就是寫(xiě)代碼了。新建好項(xiàng)目之后會(huì)在flex_src下生成一個(gè)FirstJavaFlex.mxml的文件。
我的java類的代碼如下:
view plaincopy to clipboardprint?
package com.java.flex;
public class FirstJavaFlex {
public String helloJavaFlex(String name) {
System.err.println("call java success!");
return "welcome to JavaFlex world: "+name;
}
}
package com.java.flex;
public class FirstJavaFlex {
public String helloJavaFlex(String name) {
System.err.println("call java success!");
return "welcome to JavaFlex world: "+name;
}
}
這個(gè)代碼也很簡(jiǎn)單吧不多說(shuō)了。
下面是最關(guān)鍵的一步打開(kāi)WEB-INF\flex目錄中的remoting-config.xml文件在里面
添加一段遠(yuǎn)程目錄的注冊(cè),
<destination id="firstJavaFlex">
<properties>
<source>com.java.flex.FirstJavaFlex</source>
</properties>
</destination>
添加完后的remoting-config.xml文件如下:
view plaincopy to clipboardprint?
<?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="firstJavaFlex">
<properties>
<source>com.java.flex.FirstJavaFlex</source>
</properties>
</destination>
</service>
<?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="firstJavaFlex">
<properties>
<source>com.java.flex.FirstJavaFlex</source>
</properties>
</destination>
</service>
接下來(lái)修改FirstJavaFlex.mxml,代碼如下:
view plaincopy to clipboardprint?
<?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.controls.Alert;
import mx.rpc.events.ResultEvent;
public function callJava():void {
remoteHello.dd(myName.text);
remoteHello.addEventListener(ResultEvent.RESULT,getRomoteHelloRes);
}
private function getRomoteHelloRes(e: ResultEvent):void{
Alert.show(e.result.toString());
}
private function error(e:FaultEvent):void {
eerrorMsg.text=e.message.toString();
}
]]-->
</mx:Script>
<mx:RemoteObject destination="helloJavaFlex" id="remoteHello" fault="error(event)">
</mx:RemoteObject>
<mx:Button x="338" y="103" label="call java" click="callJava()"/>
<mx:TextInput x="155" y="103" id="myName"/>
<mx:Text x="78" y="105" text="your name:
" height="20" width="74"/>
<mx:Text x="56" y="174" text="error message:" height="20" width="96"/>
<mx:TextArea id="errorMsg" width="256" height="200" x="155" y="173">
</mx:TextArea>
</mx:Application>
<?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.controls.Alert;
import mx.rpc.events.ResultEvent;
public function callJava():void {
remoteHello.dd(myName.text);
remoteHello.addEventListener(ResultEvent.RESULT,getRomoteHelloRes);
}
private function getRomoteHelloRes(e: ResultEvent):void{
Alert.show(e.result.toString());
}
private function error(e:FaultEvent):void {
errorMsg.text=e.message.toString();
}
]]-->
</mx:Script>
<mx:RemoteObject destination="helloJavaFlex" id="remoteHello" fault="error(event)">
</mx:RemoteObject>
<mx:Button x="338" y="103" label="call java" click="callJava()"/>
<mx:TextInput x="155" y="103" id="myName"/>
<mx:Text x="78" y="105" text="your name:
" height="20" width="74"/>
<mx:Text x="56" y="174" text="error message:" height="20" width="96"/>
<mx:TextArea id="errorMsg" width="256" height="200" x="155" y="173">
</mx:TextArea>
</mx:Application>
最后界面是這樣的,如下圖:

簡(jiǎn)單解釋一下:
界面上有一個(gè)TextInput用來(lái)輸入調(diào)用遠(yuǎn)程java類的參數(shù)。TextArea用來(lái)顯示調(diào)用不成功的錯(cuò)語(yǔ)信息。Button的click事件用來(lái)觸發(fā)調(diào)用遠(yuǎn)程java方法。還有一個(gè)在界面上不顯示的RemoteObject對(duì)象是用來(lái)注冊(cè)遠(yuǎn)程java目標(biāo)的。
好了運(yùn)行一下吧。
右鍵WebContent目錄中的FirstJavaFlex.html文件選擇Run as->Run on Server.看一下運(yùn)行結(jié)果吧。如下圖:

點(diǎn)擊一下call java 按鈕,看什么什么情況?如下圖:

恭喜你成功了!
這個(gè)例子很簡(jiǎn)單,但因?yàn)椴皇欤乙彩钦{(diào)了好久才成功。容易出錯(cuò)的地址就好個(gè)Context的問(wèn)題了。當(dāng)然也可以在RemoteObject中明確聲明EndPoint屬性,建議使用相對(duì)路徑。
<mx:RemoteObject destination="firstJavaFlex" id="remoteHello" fault="error(event)" endpoint="messagebroker/amf"/>
就寫(xiě)到這吧,下一篇準(zhǔn)備寫(xiě)寫(xiě)在真實(shí)開(kāi)發(fā)中會(huì)用到的與spring結(jié)合的使用。
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/zk_2000/archive/2009/11/02/4759007.aspx