轉載地址:
http://www.yeeach.com/2009/07/21/%e4%bd%bf%e7%94%a8blazeds%e5%ae%9e%e7%8e%b0java%e5%92%8cflex%e9%80%9a%e4%bf%a1%e4%b9%8bhello-world/
新的項目對用戶體驗及用戶互動要求較高,決定選用Flex作為前端的展現技術,整體框架仍然是Flex+Spring+Hibernate(考慮采用seam中)。作為入門,先從經典的Hello world開始,暫時不考慮Flex與Spring、Hibernate的集成。
Flex要實現與Java集成,開源項目BlazeDS、GraniteDS、Flamingo都提供了相應的解決方案,考慮到BlazeDS是Adobe官方的開源項目,因此采用BlazeDs作為Flex與Java通信的基礎框架。什么是BlazeDS呢,看看官方的介紹:
BlazeDS is the server-based Java remoting and web messaging technology that enables developers to easily connect to back-end distributed data and push data in real-time to Adobe® Flex® and Adobe AIR™ applications for more responsive rich Internet application (RIA) experiences.
開發工具采用Eclipse+Flex Builder 3 Plug-in方式,不采用Flex Builder 3。先安裝Eclipse,再安裝Flex Builder 3 Plug-in,相關的安裝配置不再贅述。
1、下載BlazeDS
下載BlazeDS Turnkey :http://flexorg.wip3.adobe.com/blazeds/3.0.x/milestone/3978/blazeds-turnkey-3.2.0.3978.zip
由于BlazeDS Turnkey中包含BlazeDS的使用例子,對于入門熟悉Flex及BlazeDS都有較好的參考價值,因此建議下載BlazeDS Turnkey。
關于blazeds-turnkey 的目錄說明:
docs:BlazeDS Javadoc
resources:BlazeDS的相關支持包,包括clustering(采用jgroups)、BlazeDS與ColdFusion 集成的配置文件、BlazeDS的配置文件、BlazeDS與AJAX集成的橋、Flex的SDK、Flex的java library、BlazeDS與Tomcat、Jboss、Websphere等security集成的支持包。
sampledb:hsqldb的啟動腳本及樣例數據庫
tomcat:Tomcat 包
blazeds.war:最小化的BlazeDS 文件,可以作為空白項目來建立BlazeDS 應用程序。
sample.war:BlazeDS的demo例子(所謂的testdrive)。
ds-console.war :BlazeDS的部署管理程序。
2、建立Java Web Project
File->New->Web Project 建立Java helloworld項目
在helloworld/src下,新建com.yeeach.HelloWorldService類,內容如下:
package com.yeeach;
public class HelloWorldService {
public String hello(String var1) {
return “hello ” + var1;
}
public String world(String var1) {
return “world ” + var1;
}
}
3、建立helloworld的BlazeDS開發環境
3.1、拷貝blazeds.war下的WEB-INF到helloworld的目錄下,覆蓋原有的WEB-INF
3.2、在helloworld下建立flex-src目錄(與src同級),用于存放flex的相關代碼
helloworld/src:用于存放項目的java代碼
helloworld/flex-src:用于存放項目flex的相關代碼
helloworld/WebRoot/WEB-INF/flex:存放flex的相關配置文件
3.3、設置Flex Project Nature



3.4、在helloworld/flex-src下,新建MXML Application :helloworld.mxml ,內容如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”vertical”>
<mx:RemoteObject destination=”com.yeeach.HelloWorldService”
id=”helloWorldService”>
<mx:method name=”hello”
result=”sayHelloResult(event)”/>
<mx:method name=”world”
result=”sayWorldResult(event)”/>
</mx:RemoteObject>
<mx:HBox>
<mx:Label text=”輸入:”/>
<mx:TextInput id=”inputStr”/>
<mx:Button label=”say hello”
click=”sayHello(event);”/>
<mx:Button label=”say world”
click=”sayWorld(event);”/>
</mx:HBox>
<mx:HBox>
<mx:Label text=”結果:”/>
<mx:TextArea id=”result”/>
</mx:HBox>
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
function sayHello(event:Event):void
{
var inputVar:String=inputStr.text;
helloWorldService.hello(inputVar);
}
function sayWorld(event:Event):void
{
var inputVar:String=inputStr.text;
helloWorldService.world(inputVar);
}
private function sayHelloResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回結果");
}
private function sayWorldResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回結果");
}
]]>
</mx:Script>
</mx:Application>
3.5、修改remoting-config.xml,增加對destination的說明
<destination id=”com.yeeach.HelloWorldService”>
<properties>
<source>com.yeeach.HelloWorldService</source>
</properties>
</destination>
3.6、設置Flex Build Path等相關屬性
1)右鍵->Properties,設置Flex Build Path屬性,將Main source folder修改為flex-src,然后點擊“OK”
2)右鍵->Properties,設置Flex Applications屬性,添加flex-src下的其他Application,然后點擊“OK”
如果需要添加flex-src子目錄下的其他Application(例如helloworld/flex-src/com/yeeach/helloworld1.mxml),目前從UI界面似乎無法正確添加,可以直接修改.actionScriptProperties,在<applications></applications>中間增加相應的Application
<applications>
<application path=”helloworld.mxml”/>
<application path=”com/yeeach.com/helloworld1.mxml”/>
</applications>
3)右鍵->Properties,設置Flex Compiler屬性,將Flex SDK version 修改為“Use default”或“Use a specific SDK”,指向正確的Flex SDK;確認“Additional compiler arguments”配置參數正確,然后點擊“OK”
4)右鍵->Properties,設置Flex Server屬性,配置為正確的參數,然后點擊“OK”

3.7、部署helloworld 應用到Tomcat
通過http://127.0.0.1:8080/helloworld/helloworld.swf來訪問我們的hello world
3.8、分析helloworld.mxml
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”vertical”>
<mx:RemoteObject destination=”com.yeeach.HelloWorldService”
id=”helloWorldService”>
//此處的destination=”com.yeeach.HelloWorldService”與remoting-config.xml中的id=”com.yeeach.HelloWorldService”完全匹配
//id=”helloWorldService”用來在actionscript中標識destination=”com.yeeach.HelloWorldService”,后面的helloWorldService.hello(inputVar)等都使用此id;
<mx:method name=”hello”
result=”sayHelloResult(event)”/>
//mx:method 聲明java類com.yeeah.com.HelloWorldService中的hello方法及響應結果回調函數sayHelloResult
<mx:method name=”world”
result=”sayWorldResult(event)”/>
</mx:RemoteObject>
<mx:HBox>
<mx:Label text=”輸入:”/>
<mx:TextInput id=”inputStr”/>
<mx:Button label=”say hello”
click=”sayHello(event);”/>
<mx:Button label=”say world”
click=”sayWorld(event);”/>
</mx:HBox>
<mx:HBox>
<mx:Label text=”結果:”/>
<mx:TextArea id=”result”/>
</mx:HBox>
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
function sayHello(event:Event):void
{
var inputVar:String=inputStr.text;
helloWorldService.hello(inputVar);
}
function sayWorld(event:Event):void
{
var inputVar:String=inputStr.text;
helloWorldService.world(inputVar);
}
private function sayHelloResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回結果");
}
private function sayWorldResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回結果");
}
]]>
</mx:Script>
</mx:Application>
代碼文件:helloworld.rar