2011-02-18

BlazeDS

最近用力的在學Flex,而為了要讓Flex連到後端程式來做資料庫的存取動作,選擇了使用BlazeDS這個免費且開放源始碼的技術,底下介紹如何建立一個簡單的範例。



開發環境
Java Virtual Machines
l          JDK 1.5 或更高

Application Servers
l          Java Servlet container supporting servlet 2.3 或更高

Flex/AIR Applications using BlazeDS
l          Flex SDK 3.2 build 3794 (3.0.2.2.14193)
l          AIR 1.1 runtime
l          Flash Player 9

下載BlazeDS
Tomcat   BlazeDS所需的Jar  原始碼


利用eclipse tomcat plugin建立Tomcat Project
將下載的BlazeDS blazeds.war解開,複製其WEB-INF下所有檔案至Project


建立Hello.java
package hello;
public class HelloWorld {
        public String sayHelloTo(String str) {
                System.out.println("Hello " + str);
                return "Hello " + str;
        }
        public String test() {
                return "HelloWorld";
        }
}
remote-config.xml增加

<destination id="helloworld">
  <properties>
   <source>hello.HelloWorld</source>
  </properties>
 </destination>


建立好打開Tomcat測試,自行測試是否能運行

接下利用Flex Builder (至官方網站下載試用版) 來建立Flex Project
          
    Application server type選擇J2EE         

    設定路徑,設定後好點選Validate Configuration是否連結成功
撰寫mxml內容

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 <mx:Script>

 <![CDATA[

 import mx.rpc.events.ResultEvent;
 import mx.rpc.events.FaultEvent
 import mx.utils.ObjectUtil;
 import mx.controls.Alert;
 import mx.utils.StringUtil;

 private function resultHandler(event:ResultEvent):void {

 //used for debugging - shows details about result
 //returned by the Java class method
 Alert.show( ObjectUtil.toString(event.result) );
 
 }

 private function faultHandler(event:FaultEvent):void {
  Alert.show( ObjectUtil.toString(event.fault) );
 }

 ]]>

 </mx:Script>
 <mx:RemoteObject id="ro" destination="helloworld"  source="hello.HelloWorld" result="resultHandler(event)"
  fault="faultHandler(event)"/>
 <mx:Button label="Get Hello World!" click="ro.test()"/>
</mx:Application>



destination是在remote-config.xml所建立的
呼叫HelloWorld裡的test

直接執行Flex Project
設定沒問題的話畫面會自動導至
執行畫面



0 意見:

張貼留言