创建Flex 与 Java 通信项目之Blazeds篇
1、blazeds_turnkey(内含tomcat)
2、Flex Build3(Flex的eclipse插件)
3、Eclipse
注意:创建项目完之后,将其部署到Tomcat中,为配置Flex类型做准备
导入方法有两种:
一种是,打开下载到的blazeds_turnkey,找到blazeds.war压缩包,copy里面的WEB-INF文件到web项目WebRoot目录下,覆盖原先的WEB-INF目录;
另一种,软件导入。
(1) 右击项目” WebRoot” 目录,选择”Import”->”File system”
(2)在From directory选项中选择blazeds.war解压缩的路径
(3)选中 “blazeds”,点击”finish”
(4)出现Question提示,点击”Yes to All”
1、创建Flex_src目录,用于,专门存放flex的源代码文。(为后面修改Flex配置做准备)
如下图:
2、添加Flex项目类型
(1) 右击FlexBlazedsDemo项目,选择 “Flex Projext Nature ”->”Add Flex Project Nature”
(2)点击点击”Next”
(3)配置属性:
Root folder:指的是项目发布的目录location(选择刚部署到Tomcat中的FlexBlazedsDemo)
Root URL:指定是项目的发布的根URL地址(访问路径)
Context root:指定是项目名
Output folder:Flex编译后HTML文件存放位置
然后点击Valdate Configuration按钮,没有任何错误提示点击Finish即完成项目创建,此flex项目中
注意:
如果Tomcat是启动状态,点击Valdate Configuration会出现:“The web root folder and root URl are valid”。(蓝色提示)
如果是关闭状态则会出现“Cannot access the web server. The server may not b…or the web root folder or root URL may be invalid”(黄色提示)
这要不是红色提示就没有问题
(4)修改Flex配置
右击FlexBlazedsDemo项目,选择 “Properties”
选择“Flex build Path”,将Main source folder改为前面创建的flex_src文件夹。
选择“Flex Cpmpiler” 将Flex SDK version 改为Flex插件的版本号,这里是Flex3.2
注意:如果不修改Flex SDK version项目可能会出现红叉
点击“Apply”->“OK”
将src目录下的FlexBlazedsDemo.mxml文件移至到flex_sr此目录下。
package com.flex;
public class FlexBlazeds { private String prefix;
public FlexBlazeds() { this.prefix = "FlexBlazeds_"; }
public String say(String str) {
return this.prefix + str;
}
public String getPrefix() { return prefix; }
public void setPrefix(String prefix) { this.prefix = prefix; } }
|
代码如下:
<?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="flexBlazedsRemoteObject"> <properties> <source>com.flex.FlexBlazeds</source> <scope>application</scope> </properties> </destination> </service> |
注意:红色部分为添加部分
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent;
[Bindable]
private var meg:String = "";
private function clickHandler():void { //remoteObject是 <mx:RemoteObject/>中的属性id remoteObject.say(inputText.text);
} //返回成功 private function resultHandler(event:ResultEvent):void{
meg = event.result as String;
} //返回失败 private function faultHandler(event:FaultEvent):void{ Alert.show("faultHandler"); } ]]>
</mx:Script>
<mx:RemoteObject id="remoteObject" destination="flexBlazedsRemoteObject" result="resultHandler(event)" fault="faultHandler(event)"/> <mx:HBox x="10" y="20"> <mx:TextInput id="inputText"/> <mx:Button click="clickHandler()" label="测试" /> <mx:Label text="{meg}" /> </mx:HBox> </mx:Application>
|
注意:<mx:RemoteObject/> 标签中destination属性值就是remoting-config.xml文件中destination标签中的id
web.xml配置(会自动加载):
<display-name>BlazeDS</display-name> <description>BlazeDS Application</description> <context-param> </listener> |
ps:如果报错,更换flex SDK version
http://localhost:8080/FlexBlazedsDemo/FlexBlazedsDemo-debug/FlexBlazedsDemo.html
结果如下:
Java环境的搭建,Eclipse软件的安装、Flex Build3安装省略。