Flex+BlazeDS+java通信详细笔记

Posted on 2013-09-26 09:38  诸葛小北  阅读(643)  评论(1编辑  收藏  举报

整了很长时间的通信,还是一直有一点问题。现在搞定了,记录一下,也跟有需求的同学们共享。

我重新把所有的过程再做一遍。

1新建Flex+BlazeDS+JAVA项目

右键、新建Flex项目

其中blazeds.war提供下载,你可以去官网,或者从我这里:

http://yunpan.cn/QGzSwiyinUFiS  访问密码 25a8

 

2结构分析

flex_src是前台flex包

src是后台java包

WebRoot是输出文件包

重点就在WEB-INF

里边有blazeDS的lib,有flex的配置文件,包括messageing-config.xml,proxy-config.xml,remoting-config.xml,services-config.xml

 3 写后台

package com;

public class HelloWorld {
  
    public String getString(String name){
        System.out.println("daole");
        return "这里是后台-:-这里是前台"+name;
    }
}

4写配置文件--修改WebRoot》WEB-INF》flex>remoting-config.xml

增加

  <destination id="helloWorld">
      <properties>
        <source>com.HelloWorld</source>
      </properties>
    </destination>

 

 5写前台

<?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="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            
            protected function resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                Alert.show("进入result");
                Alert.show(event.result.toString(),"提示");
                
            }
            
            protected function faultHandler(event:FaultEvent):void
            {
                // TODO Auto-generated method stub
                Alert.show("进入faultHandler");
                Alert.show(event.fault.toString(),"提示");
                
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <mx:RemoteObject id="remoteObject" destination="helloWorld" showBusyCursor="true" source="com.HelloWorld" result="resultHandler(event)" fault="faultHandler(event)"/>
    </fx:Declarations>
    <mx:Button label="发送" click="remoteObject.getString('nndx')" x="139" y="126"/>
    
</s:Application>
View Code

其中,RemoteObject 的id是之后要引用的函数名,destination对应配置文件的引用名。

6按理说到这里前后台就配置完成了,但是还有一点点小问题,我们运行一下,在Tomcat右键Add Deployment。运行Tomcat

发现程序进入了FAULT,没有进入RESULT,通道URL是url: 'http://localhost:8080/WebRoot/messagebroker/amf'

明显是路径错了,我们运行项目的路径是http://localhost:8080/PushTest6/PushTest6.html,结果被替换成了WebRoot。

Debug,找到如下图路径的配置文件

 问题来了,通道是默认的,都是变量,可以替换

 <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>

{context.root}被替换成了WebRoot了呗。

先手动改了my-amf的通道路径看看能不能用。

 <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <!-- <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/> -->
            <endpoint url="http://{server.name}:{server.port}/PushTest6/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>

这里要注意重新清理项目,否则配置文件仍然是默认的。

 

明显修改了路径是可以的。但这样修改似乎不太好。我们想办法修噶器ontext.root变量。找到项目文件夹的.flexProperties文件,笔记本打开。

把serverContextRoot="/WebRoot" 改成serverContextRoot="/PushTest6"

成功了。改回来,看看还有没有别的办法。

办法2:

项目右键,属性,

附加的编译参数,增加

-context-root "\PushTest6"
-services "D:/Workspace/PushTest6/WebRoot/WEB-INF/flex/services-config.xml" -locale en_US
-context-root "\PushTest6"

报错说配置变量compiler.contex-root仅能设置一次。看来要把.flexProperties的删掉才行

试试看,

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<flexProperties enableServiceManager="false" flexServerFeatures="4" flexServerType="8" flexWarLocation="D:/Workspace/lib/blazeds.war" serverContextRoot="/WebRoot" serverRoot="D:/Workspace/PushTest6/WebRoot" serverRootURL="http://localhost:8080/PushTest6" toolCompile="true" useServerFlexSDK="false" version="2"/>

修改为

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<flexProperties enableServiceManager="false" flexServerFeatures="4" flexServerType="8" flexWarLocation="D:/Workspace/lib/blazeds.war"  serverRoot="D:/Workspace/PushTest6/WebRoot" serverRootURL="http://localhost:8080/PushTest6" toolCompile="true" useServerFlexSDK="false" version="2"/>

解决了。

 

 

7穿插解决一个小问题

由于“RemoteObject”声明未实现“mx.core.IUIComponent”,它必须包含在 <Declarations> 标签中 怎么解决

把 RemoteObject 标签放到 <Declarations>标签中就可以了,如:
<fx:Declarations>
<mx:RemoteObject id="service" destination="fluorine" source="FlexDotNet.ServiceLibrary.Sample">
<mx:method name="Echo" result="onResult(event)">
</mx:method>
</mx:RemoteObject>
</fx:Declarations>


8增加一个能够输入参数的代码
前台
<?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="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            
            protected function resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                Alert.show("进入result");
                Alert.show(event.result.toString(),"提示");
                
            }
            
            protected function faultHandler(event:FaultEvent):void
            {
                // TODO Auto-generated method stub
                Alert.show("进入faultHandler");
                Alert.show(event.fault.toString(),"提示");
                
            }
            
            protected function remotingSayHello(event:Event):void
            {
                // TODO Auto-generated method stub
                var iname:String = tiName.text;
                say.getHelloWorld(iname);
                Alert.show("1");
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <mx:RemoteObject id="remoteObject" destination="helloWorld" showBusyCursor="true" source="com.HelloWorld" result="resultHandler(event)" fault="faultHandler(event)"/>
        <mx:RemoteObject id="say" destination="helloWorld">    </mx:RemoteObject>
    </fx:Declarations>
    
    <mx:Button label="发送" click="remoteObject.getString('nndx')" x="539" y="126"/>
    
    
    <s:Button x="335" y="80"  label="Click" click="remotingSayHello(event)"/>
    <s:TextInput x="159" y="80" id="tiName"/>
    <s:Label x="109" y="82" text="name:"/>
    <mx:Label text="{say.getHelloWorld.lastResult}" x="44" y="162" width="448" height="71" id="lblView" color="#FCEE09" fontSize="20" fontWeight="bold" textDecoration="underline" fontStyle="normal"/>
    
</s:Application>
View Code

后台

package com;

public class HelloWorld {
  
    public String getString(String name){
        System.out.println("daole");
        return "这里是后台-:-这里是前台"+name;
    }
    
       public String getHelloWorld(String name){
            System.out.print("执行到了getHelloWorld这里");
             return "HelloWorld!"+name;
            
         }
}
View Code

源代码分享一下吧:

 

 http://yunpan.cn/QGzvBUYieXvVS  访问密码 20d7

请尊重作者劳动成果。

 

 

 

 

Copyright © 2024 诸葛小北
Powered by .NET 8.0 on Kubernetes