開發(fā)環(huán)境:
System:Windows
WebBrowser:IE6+、Firefox3+
JavaEE Server:tomcat5.0.2.8、tomcat6
IDE:eclipse、MyEclipse 8
Flex IDE:Flash Builder 4
BlazeDS:4.5
開發(fā)依賴庫:
JavaEE5、blazeDS 4.5
Email:hoojo_@126.com
Blog:http://blog.csdn.net/IBM_hoojo
http://hoojo.cnblogs.com/
一、準備工作
1、 首先要提供相關(guān)的jar包
Java服務(wù)器端需要提供BlazeDS相關(guān)的配置和jar包
下載地址:http://opensource.adobe.com/wiki/display/blazeds/download+blazeds+trunk
下載后,解壓你可以看到這樣的一個目錄
Docs就是文檔
Resource是源碼
SampleDB是示例用的數(shù)據(jù)庫,可以運行startdb.bat來啟動數(shù)據(jù)庫
Tomcat是內(nèi)置的tomcat,如果你沒有tomcat的話可以使用它,在tomcat的webapps目錄中有samples示例
blazeds.war就是blazeDS的核心文件、庫,你可以把這個war放到tomcat的webapps目錄下,就會自動解壓。當然你也可以自己手動解壓。
Blazeds-spring.war是和spring整合的配置
Ds-console.war是blazeDS的控制臺程序
Samples.war是官方提供的示例
Samples-spring.war是spring和blazeDS的整合示例
二、部署服務(wù)器端程序
1、新建一個JavaWeb Project工程,然后在WEB-INF/lib目錄中添加如下jar包
這些jar包可以在blazeds.war包中的lib目錄中可以找到
2、 然后你需要將blazeds.war包中的WEB-INF目錄下的flex目錄復(fù)制到當前工程的WEB-INF下
3、 將blazeds.war包中的WEB-INF目錄下的web.xml的配置,添加到當前工程的web.xml文件中
4、 最后基本的樣式如下
5、 最后你發(fā)布當前工程,如果沒有錯誤就表明你服務(wù)器端部署成功了。
6、 編寫一個HelloWorld的java程序。代碼如下
package com.hoo.flex;
/**
* <b>function:</b> HelloWorld Example
* @author hoojo
* @createDate 2011-8-31 下午06:11:27
* @file HelloWorld.java
* @package com.hoo.flex
* @project BlazeDSServer
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class HelloWorld {
public HelloWorld() {
}
public String sayHello(String name) {
return "[" + name + "] say hello!";
}
}
就一個sayHello方法,接收一個參數(shù)。
三、Flex客戶端程序
1、創(chuàng)建一個Flex工程,在選擇服務(wù)器技術(shù)的時候,你需要選擇J2EE。然后勾上使用J2EE技術(shù),然后選擇BlazeDS。點擊Next下一步
2、配置根文件夾,也就是JavaEE服務(wù)器端發(fā)布程序在tomcat中的位置。我這里是在tomcat的webapps的BlazeDSServer中,BlazeDSServer是我的服務(wù)器端程序。根URL是訪問服務(wù)器端程序的url;上下文目錄對應(yīng)工程名稱;最后就是輸出文件夾目錄,這個是Flex的文件最后在tomcat中保存的目錄。
3、最后你需要設(shè)置服務(wù)器端的services-config.xml的路徑到編譯參數(shù)中,這個很重要!如果你不設(shè)置的話,那么你在后面用RemoteObject調(diào)用BlazeDS的時候,就需要設(shè)置endpoint。設(shè)置如下:
-services是參數(shù)鍵,后面的字符串是值。我這里是設(shè)置BlazeDSServer發(fā)布到tomcat目錄中的services-config.xml的路徑。
4、編譯Flex前端代碼,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="BlazeDSHelloWorld.mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
private function faultHandler(event: Event): void {
Alert.show(event.toString(), event.type);
}
private function resultHandler(event: ResultEvent): void {
//event.result是服務(wù)器端返回對象
result.text = "Message:" + event.result.toString();
}
private function sendHandler(): void {
helloRemoteObject.sayHello(userName.text);
}
]]>
</mx:Script>
<!-- 當工程沒有設(shè)置編譯器-service參數(shù) 或是-context-root等參數(shù),就需要手動設(shè)置endpoint參數(shù) -->
<mx:RemoteObject
id="helloRemoteObject"
destination="helloWorld"
fault="faultHandler(event)"
result="resultHandler(event)"
showBusyCursor="true"/>
<mx:Panel x="10" y="10" width="272" height="148" layout="absolute" title="BlazeDS Remote HelloWorld Sample">
<mx:Label x="10" y="22" text="請輸入名稱"/>
<mx:TextInput x="70" y="19" id="userName"/>
<mx:Button x="184" y="45" label="發(fā)送" click="sendHandler()"/>
<mx:Text x="10" y="79" id="result"/>
</mx:Panel>
</mx:Application>
首先你需要將Java服務(wù)器端的HelloWorld程序配置在flex的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="helloWorld">
<properties>
<source>com.hoo.flex.HelloWorld</source>
</properties>
</destination>
</service>
上面mxml代碼中的RemoteObject的destination對應(yīng)的就是remoting-config.xml配置文件中的destination的id。這個是一一對應(yīng)的,然后在sendHandler方法中,helloRemoteObject對應(yīng)的就是RemoteObject的id,而sayHello方法對應(yīng)的就是配置在remoting-config.xml中的destination的source的Java服務(wù)器端代碼的公有方法。添加完配置后,需要重啟tomcat。
運行上面的flex程序后,如果輸入?yún)?shù)后,點擊發(fā)送,可以看到服務(wù)器端返回的消息就說明BlazeDS整合Flex成功了。
版權(quán)所有,轉(zhuǎn)載請注明出處 本文出自:
版權(quán)所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明出處,謝謝