Flex4系列二:Flex4和Java通信的简单应用
??????? 搞了好久,感觉是插件安装的问题,因为我的问题比较奇怪,下一篇文章写写问题的解决方案。
??????? 这篇写一个简单的demo。flex4和java通信是必须的。
??????? 一:java类:
?????????????
package test;//flex4调用的java类public class SayHello {public String sayHelloToYou(String name){return "hello,"+name;}}
???????? 二:webRoot目录下导入blazeds的war解压以后的文件,导入后如图。当然可以手动加入,不过最好是能满足这个结构,方便另外一些配置。
????????
?
??????? 三:部署到服务器,然后启动服务器,这个很重要,因为在添加flex支持的时候需要检测服务器路径
????????????? 然后添加flex的支持,选择BlazeDS
?????????????
?
????????? 点击next,进行BlaszeDS服务器的配置
???
?????????
?
??????? 四:在Blazeds.war中有四个配置文件,demo中只用2个remoting-config.xml和services-config.xml
???????? remoting-config.xml
????????
<?xml version="1.0" encoding="UTF-8"?><service id="remoting-service" default="true"/> </adapters> //参照必须 <default-channels> <channel ref="my-amf"/> </default-channels> //以下为与java通信 <destination id="mytest"> <properties> <source>test.SayHello</source> </properties> </destination></service>
??? services-config.xml
?????
???
<?xml version="1.0" encoding="UTF-8"?><services-config> <services> <service-include file-path="remoting-config.xml" /> </services> <channels> <channel-definition id="my-amf" name="code"><?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1024" minHeight="768"><fx:Script><![CDATA[import mx.controls.Alert;import mx.rpc.events.FaultEvent;import mx.rpc.events.ResultEvent;protected function myFlex_resultHandler(event:ResultEvent):void{var name:String=event.result as String;Alert.show(name);}protected function btnSend_clickHandler(event:MouseEvent):void{myFlex.sayHelloToYou(txtName.text);}]]></fx:Script><fx:Declarations><mx:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)"/></fx:Declarations><s:Panel x="87" y="20" width="250" height="200" title="hello,everybody!"><s:TextInput x="16" y="37" id="txtName"/><s:Button x="168" y="38" label="发送" id="btnSend" click="btnSend_clickHandler(event)"/></s:Panel></s:Application>
?
??? 五:以上步骤都是在tomcat开着的情况下进行的。
????????? 但是,可能是我插件的问题吧。总是无法自动编译。所以我手动编译了
????????? 编译好以后在项目中会有bin-debug文件夹,里面有与项目同名的flash文件,双击即可运行。
?????????
??????
????????
?
???? 如果一切顺利的话,最后效果就是:
???
???
?
?
ok。就这样吧。还有些比较奇怪的问题,请看下回分解!
?
?
?????