瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          world
          轉(zhuǎn)載地址: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/

          新的項(xiàng)目對(duì)用戶體驗(yàn)及用戶互動(dòng)要求較高,決定選用Flex作為前端的展現(xiàn)技術(shù),整體框架仍然是Flex+Spring+Hibernate(考慮采用seam中)。作為入門,先從經(jīng)典的Hello world開始,暫時(shí)不考慮Flex與Spring、Hibernate的集成。

          Flex要實(shí)現(xiàn)與Java集成,開源項(xiàng)目BlazeDSGraniteDSFlamingo都提供了相應(yīng)的解決方案,考慮到BlazeDS是Adobe官方的開源項(xiàng)目,因此采用BlazeDs作為Flex與Java通信的基礎(chǔ)框架。什么是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.

          開發(fā)工具采用Eclipse+Flex Builder 3 Plug-in方式,不采用Flex Builder 3。先安裝Eclipse,再安裝Flex Builder 3 Plug-in,相關(guān)的安裝配置不再贅述。

          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的使用例子,對(duì)于入門熟悉Flex及BlazeDS都有較好的參考價(jià)值,因此建議下載BlazeDS Turnkey。

          關(guān)于blazeds-turnkey 的目錄說明:

          docs:BlazeDS Javadoc

          resources:BlazeDS的相關(guān)支持包,包括clustering(采用jgroups)、BlazeDS與ColdFusion 集成的配置文件、BlazeDS的配置文件、BlazeDS與AJAX集成的橋、Flex的SDK、Flex的java library、BlazeDS與Tomcat、Jboss、Websphere等security集成的支持包。

          sampledb:hsqldb的啟動(dòng)腳本及樣例數(shù)據(jù)庫

          tomcat:Tomcat 包

          blazeds.war:最小化的BlazeDS 文件,可以作為空白項(xiàng)目來建立BlazeDS 應(yīng)用程序。

          sample.war:BlazeDS的demo例子(所謂的testdrive)。

          ds-console.war :BlazeDS的部署管理程序。

          2、建立Java Web Project

          File->New->Web Project 建立Java helloworld項(xiàng)目

          blazeds1 在helloworld/src下,新建com.yeeach.HelloWorldService類,內(nèi)容如下:

          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開發(fā)環(huán)境

          3.1、拷貝blazeds.war下的WEB-INF到helloworld的目錄下,覆蓋原有的WEB-INF

          3.2、在helloworld下建立flex-src目錄(與src同級(jí)),用于存放flex的相關(guān)代碼

          blazeds2 helloworld/src:用于存放項(xiàng)目的java代碼

          helloworld/flex-src:用于存放項(xiàng)目flex的相關(guān)代碼

          helloworld/WebRoot/WEB-INF/flex:存放flex的相關(guān)配置文件

          3.3、設(shè)置Flex Project Nature

          blazeds3

          blazeds4

          blazeds5

          3.4、在helloworld/flex-src下,新建MXML Application :helloworld.mxml  ,內(nèi)容如下:

          <?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=”結(jié)果:”/>
          <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(), "返回結(jié)果");
          }

          private function sayWorldResult(event:ResultEvent):void
          {
          result.text=event.result.toString();
          Alert.show(event.result.toString(), "返回結(jié)果");
          }
          ]]>
          </mx:Script>
          </mx:Application>

          3.5、修改remoting-config.xml,增加對(duì)destination的說明

          <destination id=”com.yeeach.HelloWorldService”>
          <properties>
          <source>com.yeeach.HelloWorldService</source>
          </properties>
          </destination>

          3.6、設(shè)置Flex Build Path等相關(guān)屬性

          1)右鍵->Properties,設(shè)置Flex Build Path屬性,將Main source folder修改為flex-src,然后點(diǎn)擊“OK”

          2)右鍵->Properties,設(shè)置Flex Applications屬性,添加flex-src下的其他Application,然后點(diǎn)擊“OK”

          如果需要添加flex-src子目錄下的其他Application(例如helloworld/flex-src/com/yeeach/helloworld1.mxml),目前從UI界面似乎無法正確添加,可以直接修改.actionScriptProperties,在<applications></applications>中間增加相應(yīng)的Application

          <applications>
          <application path=”helloworld.mxml”/>

          <application path=”com/yeeach.com/helloworld1.mxml”/>
          </applications>

          3)右鍵->Properties,設(shè)置Flex Compiler屬性,將Flex SDK version 修改為“Use default”或“Use a specific SDK”,指向正確的Flex SDK;確認(rèn)“Additional compiler arguments”配置參數(shù)正確,然后點(diǎn)擊“OK”

          blazeds6 4)右鍵->Properties,設(shè)置Flex Server屬性,配置為正確的參數(shù),然后點(diǎn)擊“OK”

          blazeds7

          3.7、部署helloworld 應(yīng)用到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中標(biāo)識(shí)destination=”com.yeeach.HelloWorldService”,后面的helloWorldService.hello(inputVar)等都使用此id;

          <mx:method name=”hello”
          result=”sayHelloResult(event)”/>

          //mx:method 聲明java類com.yeeah.com.HelloWorldService中的hello方法及響應(yīng)結(jié)果回調(diào)函數(shù)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=”結(jié)果:”/>
          <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(), "返回結(jié)果");
          }

          private function sayWorldResult(event:ResultEvent):void
          {
          result.text=event.result.toString();
          Alert.show(event.result.toString(), "返回結(jié)果");
          }
          ]]>
          </mx:Script>
          </mx:Application>

          代碼文件:helloworld.rar


          評(píng)論

          # re: 使用BlazeDS實(shí)現(xiàn)Java和Flex通信(轉(zhuǎn)載)[未登錄]  回復(fù)  更多評(píng)論   

          2014-01-22 12:07 by 1
          1

          # re: 使用BlazeDS實(shí)現(xiàn)Java和Flex通信(轉(zhuǎn)載)[未登錄]  回復(fù)  更多評(píng)論   

          2014-03-17 16:00 by a
          a
          主站蜘蛛池模板: 申扎县| 邻水| 于都县| 斗六市| 富源县| 丹江口市| 霍林郭勒市| 祁门县| 禹州市| 延庆县| 博客| 凌海市| 苏尼特右旗| 兴安盟| 泰来县| 克山县| 呼图壁县| 怀柔区| 乌恰县| 武功县| 沁阳市| 铁岭县| 巴青县| 景德镇市| 灵丘县| 邻水| 邯郸市| 富顺县| 农安县| 德阳市| 托克逊县| 防城港市| 孟州市| 吉首市| 夹江县| 罗甸县| 棋牌| 丹凤县| 普定县| 林周县| 广宁县|