1、下載 blazeds 4
http://opensource.adobe.com/wiki/display/blazeds/download+blazeds+trunk
2、創建java project(web project)
-創建testapp類
package zj.services;
public class testapp {
public testapp() {
}
public String callHello(String name) {
return "hello,"+name;
}
}
-解壓 blazeds.war 複製到項目根目錄。
此時目錄結構:
---src
---WEB-INF
----flex
----messaging-config.xml
----proxy-config.xml
----remoting-config.xml (需修改)
----services-config.xml
----version.properties
----lib
----src
----web.xml (需修改)
-修改web.xml文件
添加RDSDispatchServlet的聲明和映射
<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id="RDS_DISPATCH_MAPPING">
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>
-修改 web-inf/flex/remoting-config.xml文件
創建遠程目標(啓動Web服務的時候,BlazeDS將暴露Java類作爲一個遠程終端,將testrds作爲id)
<destination id="testrds">
<properties>
<source>zj.services</source>
</properties>
</destination>
-部署項目至tomcat下
3、創建flex項目
-對java project(web project)右鍵--添加/更改項目類型--添加flex項目類型
-配置“應用程序服務器類型”爲“j2ee”
-選擇“使用遠程對象訪問服務”,選擇“BlazeDS”
-服務器位置
根文件夾:選擇剛部署的tomcat目錄(x:\tomcat\webapps\項目名)
根URL:http://localhost:8080/項目名
上下文根目錄:/項目名
點擊“驗證配置”,“finish”配置
-切換至flash試圖(按習慣可做可不做)
爲項目創建 flex 文件目錄,將mxml文件放到此文件夾中,爲和java文件區分,便於管理
-點擊“連接數據/服務”
選擇“BlazeDS服務器目標”,選擇之前在remoting-config.xml配置中的遠程終端id(testrds),點擊完成。
-畫一個按鈕,對按鈕右鍵,選擇“生成服務調用”,選擇java類方法。
-畫一個文本標籤
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function button_clickHandler(event:MouseEvent):void{
callHelloResult.token = testrds.callHello(_name.text);
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="callHelloResult"/>
<services:Testrds id="testrds" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
</fx:Declarations>
<s:TextInput id="_name"/>
<s:Label id="label2" text="{callHelloResult.lastResult}"/>
<s:Button id="button" click="button_clickHandler(event)"/>