Flex+BlazeDS+IntelliJ IDEA整合開發系列一之起步demo
阿新 • • 發佈:2019-01-08
環境:blazeds-bin-4.0.1.21287,apache-tomcat-7.0.57,IntelliJ IDEA 15.0.4,Flex Builder 4.6
1. 解壓blazeds-bin-4.0.1.21287.zip,然後把blazeds.war解壓後的blazeds複製放到tomcat的webapps目錄下
2. 修改blazeds/WEB-INF/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>dcec.HelloWorld</source> </properties> </destination> </service>
3. 開啟IntelliJ IDEA,新建一個空project:temproj(IDEA的project和eclipse和MyEclipse的project不同,IDEA的project相當於eclipse/MyEclipse的workspace,而IDEA的Module相當於eclipse/MyEclipse的project),然後再建一個模組flex-client,
修改一下Main.mxml
<?xml version="1.0"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <fx:Script><![CDATA[ import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private function button1_clickHandler(event:MouseEvent):void { remoteObject.getHelloWorld(); } private function remoteObject_resultHandler(event:ResultEvent):void { Alert.show("ok", "Message"); } private function remoteObject_faultHandler(event:FaultEvent):void { Alert.show("not ok", "Message"); } ]]></fx:Script> <fx:Declarations> <s:RemoteObject id="remoteObject" destination="helloWorld" source="dcec.HelloWorld" result="remoteObject_resultHandler(event)" fault="remoteObject_faultHandler(event)"> </s:RemoteObject> </fx:Declarations> <s:Button click="button1_clickHandler(event)" label="Say Hello"/> </s:Application>
這裡添加了一個RemoteObject遠端物件,用來呼叫remoting-config.xml配置檔案裡的destination目的地
我們再新建一個module:java-obj,來寫HelloWorld這個類
然後新建一個class:dcec.HelloWorld
寫上一些程式碼
然後在開啟著的這個HelloWorld上空白地方右鍵-Compile 'HelloWorld.java',然後在專案temproj下會出現一個out資料夾,HelloWorld.class就在temproj\out\production\java-obj\dcec裡面了,返回,把dcec這個資料夾複製,然後放到tomcat/webapps/blazeds/WEB-INF/classes資料夾下,如果沒有classes,則需要先新建一個。package dcec; /** * Created by Administrator on 2016/3/31. */ public class HelloWorld { public String getHelloWorld() { System.out.println("Hello world, ndh."); return "Hello world, ndh."; } }
4. 然後啟動tomcat
5. 接著配置flex-client,這個地方由於IntelliJ IDEA版本不同可能有所差異
(Ctrl+Alt+Shift+S)File->Project Structure...->
點Apply,再點OK
6. 再配置執行時環境(Run->Edit Configurations...):
然後run一下
好了,就這樣呼叫了一個遠端物件了。