隨筆-57  評(píng)論-117  文章-1  trackbacks-0

          開發(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/

           

          一、準(zhǔn)備工作

          1、 首先要提供相關(guān)的jar包

          Java服務(wù)器端需要提供BlazeDS相關(guān)的配置和jar包

          下載地址:http://opensource.adobe.com/wiki/display/blazeds/download+blazeds+trunk

          下載后,解壓你可以看到這樣的一個(gè)目錄

          clip_image002

          Docs就是文檔

          Resource是源碼

          SampleDB是示例用的數(shù)據(jù)庫,可以運(yùn)行startdb.bat來啟動(dòng)數(shù)據(jù)庫

          Tomcat是內(nèi)置的tomcat,如果你沒有tomcat的話可以使用它,在tomcat的webapps目錄中有samples示例

          blazeds.war就是blazeDS的核心文件、庫,你可以把這個(gè)war放到tomcat的webapps目錄下,就會(huì)自動(dòng)解壓。當(dāng)然你也可以自己手動(dòng)解壓。

          Blazeds-spring.war是和spring整合的配置

          Ds-console.war是blazeDS的控制臺(tái)程序

          Samples.war是官方提供的示例

          Samples-spring.war是spring和blazeDS的整合示例

          二、部署服務(wù)器端程序

          1、新建一個(gè)JavaWeb Project工程,然后在WEB-INF/lib目錄中添加如下jar包

          clip_image004

          這些jar包可以在blazeds.war包中的lib目錄中可以找到

          2、 然后你需要將blazeds.war包中的WEB-INF目錄下的flex目錄復(fù)制到當(dāng)前工程的WEB-INF下

          3、 將blazeds.war包中的WEB-INF目錄下的web.xml的配置,添加到當(dāng)前工程的web.xml文件中

          4、 最后基本的樣式如下

          clip_image006

          5、 最后你發(fā)布當(dāng)前工程,如果沒有錯(cuò)誤就表明你服務(wù)器端部署成功了。

          6、 編寫一個(gè)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!";
              }
          }

          就一個(gè)sayHello方法,接收一個(gè)參數(shù)。

          三、Flex客戶端程序

          1、創(chuàng)建一個(gè)Flex工程,在選擇服務(wù)器技術(shù)的時(shí)候,你需要選擇J2EE。然后勾上使用J2EE技術(shù),然后選擇BlazeDS。點(diǎn)擊Next下一步

          clip_image008

          2、配置根文件夾,也就是JavaEE服務(wù)器端發(fā)布程序在tomcat中的位置。我這里是在tomcat的webapps的BlazeDSServer中,BlazeDSServer是我的服務(wù)器端程序。根URL是訪問服務(wù)器端程序的url;上下文目錄對(duì)應(yīng)工程名稱;最后就是輸出文件夾目錄,這個(gè)是Flex的文件最后在tomcat中保存的目錄。

          clip_image010

          3、最后你需要設(shè)置服務(wù)器端的services-config.xml的路徑到編譯參數(shù)中,這個(gè)很重要!如果你不設(shè)置的話,那么你在后面用RemoteObject調(diào)用BlazeDS的時(shí)候,就需要設(shè)置endpoint。設(shè)置如下:

          clip_image012

          -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ù)器端返回對(duì)象
                          result.text = "Message:" + event.result.toString();
                      }
                      
                      private function sendHandler(): void {
                          helloRemoteObject.sayHello(userName.text);
                      }        
                  ]]>
              </mx:Script>
              
              <!-- 當(dāng)工程沒有設(shè)置編譯器-service參數(shù) 或是-context-root等參數(shù),就需要手動(dòng)設(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="請(qǐng)輸入名稱"/>
                  <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對(duì)應(yīng)的就是remoting-config.xml配置文件中的destination的id。這個(gè)是一一對(duì)應(yīng)的,然后在sendHandler方法中,helloRemoteObject對(duì)應(yīng)的就是RemoteObject的id,而sayHello方法對(duì)應(yīng)的就是配置在remoting-config.xml中的destination的source的Java服務(wù)器端代碼的公有方法。添加完配置后,需要重啟tomcat。

          運(yùn)行上面的flex程序后,如果輸入?yún)?shù)后,點(diǎn)擊發(fā)送,可以看到服務(wù)器端返回的消息就說明BlazeDS整合Flex成功了。

          作者:hoojo
          出處:
          blog:http://blog.csdn.net/IBM_hoojo
                   http://hoojo.cnblogs.com
          本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。


          版權(quán)所有,轉(zhuǎn)載請(qǐng)注明出處 本文出自:
          分享道版權(quán)所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處,謝謝

          評(píng)論:
          # re: BlazeDS 整合 Flex HelloWorld 示例[未登錄] 2011-09-16 20:59 | Lee
          這東西弄過一次。  回復(fù)  更多評(píng)論
            
          # re: BlazeDS 整合 Flex HelloWorld 示例 2011-09-17 08:58 | tbw
          還沒有弄過這東西呢   回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 南华县| 定西市| 黑河市| 枣强县| 固镇县| 扶风县| 红安县| 泰和县| 江华| 平昌县| 磐安县| 宣威市| 凤山市| 济阳县| 雷州市| 麻栗坡县| 台东市| 彰化市| 凤山市| 都兰县| 自贡市| 鹿邑县| 天台县| 区。| 普定县| 鸡东县| 扎鲁特旗| 旺苍县| 牡丹江市| 黄大仙区| 延安市| 江北区| 哈尔滨市| 夹江县| 巢湖市| 精河县| 天长市| 长沙县| 同心县| 泽普县| 忻州市|