使用blazeds实现java与flex通信的简单例子实现

来源:互联网 发布:五彩精灵瓷砖.知乎 编辑:程序博客网 时间:2024/05/01 02:40

1 工具:使用 myeclipse9.0+blazeds +tomcat 6.0.35+ flash builder 4.5

 步骤:

()myeclipse端开发服务器端
1 myeclipse9.0新建一个java web project项目

  简单的书写一个包和类

代码如下:

package com.test;

publicclass HelloJavaFlex

{

                   public String helloJavaFlex(String name) {

                           return "hello,"+ name + "您可以使用javaflex通信了";

         }

}

 

2 下载blazeds的包,将解压后的web-inf下的所有文件拷贝到项目下的web-inf文件夹下,替换以前的所有文件

3 打开你新建的项目下面的WebRoot/WEB-INF/flex

下的remoting-config.xml文件

添加代码:

<!--我加入的下面代码-->

    <destinationid="helloJavaFlex">

        <properties>

        <source>com.test.HelloJavaFlex</source>

        </properties>

        </destination>

 

之后的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="helloJavaFlex">

        <properties>

        <source>com.test.HelloJavaFlex</source>

        </properties>

        </destination>

</service>

 

 

4将这个项目发布到tomcat6.0.35,这样可以得到这个项目在tomcat下的路径,这个路径就是下面flash builder 中配置blazeds时的根文件夹需要的!根据这个根文件夹,配置相应的根url和上下文根目录

本演示中,我的tomcat 6.0.35下的webapp路径为:

D:\software\apache-tomcat-6.0.35_32bit\apache-tomcat-6.0.35\webapps

因此下面的根文件夹:

D:\software\apache-tomcat-6.0.35_32bit\apache-tomcat-6.0.35\webapps\remotejf

相应的将其url配置成:

http://localhost:8081/remotejf/

上下文根目录配置成:

/remotejf

记得要使tomcat开启并且点击验证配置按钮,验证配置是否正确

输出文件夹项与在浏览器中输入的url有关

D:\software\apache-tomcat-6.0.35_32bit\apache-tomcat-6.0.35\webapps\remotejf\remotejf-debug

那么在浏览器中访问的时候的url通过上述三个配置就为
htttp://localhost:8081/remotejf/remotrjf-debug/remotrjf.html

也可以通过直接在mxml文件上右键运行方式即可得到相同的结果

 ()flashbuilder 4.5开发客户端

1 新建一个flex项目 ,名称为remotejf

2 点击下一步

点击下一步:这一步最重要!配置如下(这一步也可在写完代码后,项目->右键属性->flex服务器上去设置)

其中的根文件夹等的配置见上面

点击验证配置,出现下面的

Web根文件和根url有效 则证明配置成功!

 

其中已编译的flex应用程序的位置的

输出文件夹项可以自己选择

本演示默认为:

D:\software\apache-tomcat-6.0.35_32bit\apache-tomcat-6.0.35\webapps\remotejf\remotejf-debug

 

3 开发remote.mxml文件,代码如下:

<?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">

         <fx:Script>

                   <![CDATA[

                            importmx.rpc.events.ResultEvent;

                            importmx.controls.Alert;

                            publicfunction remotingHelloJavaFlex():void{

                                     varnameText:String=nameInputText.text;

                                     remoteHello.helloJavaFlex(nameText);

                                     remoteHello.addEventListener(ResultEvent.RESULT,getRomoteMessage);

                            }

                            privatefunction getRomoteMessage(e:ResultEvent):void{

                                     Alert.show(e.result.toString());

                            }

                   ]]>

         </fx:Script>

         <fx:Declarations>

                   <mx:RemoteObjectdestination="helloJavaFlex"

                                                         id="remoteHello"endpoint="/remotejf/messagebroker/amf">

                   </mx:RemoteObject>

         </fx:Declarations>

         <s:Labelx="144" y="104" text="姓名:"width="47"/>

         <s:TextInputx="218" y="99" id="nameInputText"/>

         <s:Buttonx="203" y="143" label="JAVA+FLEX通信"

                             click="remotingHelloJavaFlex();"/>

         <!--<mx:ColorPickerx="420" y="180"/> -->

</s:Application>

 

最后就可以在浏览器中输入地址

htttp://localhost:8081/remotejf/remotrjf-debug/remotrjf.html

或是直接运行mxml文件了,得到的结果.