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

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

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>

其中,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>

后台

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; }
}

源代码分享一下吧:

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

请尊重作者劳动成果。

Flex+BlazeDS+java通信详细笔记的更多相关文章

  1. Flex+BlazeDS+java通信详细笔记2-推送

    前台是Air,后台是java 在运行之前,先要在IE地址栏输入http://127.0.0.1:8080/PushDemo/TickCacheServlet?cmd=start 激活它. 地址:htt ...

  2. Flex和Java通信报错

    1.错误描述 11-30 18:15:52 ERROR [localhost-startStop-1] org.springframework.web.servlet.FrameworkServlet ...

  3. Flex与Java通信之HttpService

    flashbuilder4.6.myeclipse10 参考:http://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html 1. ...

  4. java 多线程详细笔记(原理理解到全部使用)

    鸽了好久以后终于又更新了,看同学去实习都是先学源码然后修改之类,才发觉只是知道语法怎么用还远远不够,必须要深入理解以后不管是学习还是工作,才能举一反三,快速掌握. 目录 基础知识 进程与线程 线程原子 ...

  5. Flex与Java交互(Flex调用java类展示数据)解析xml展示数据

    Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869 Flex与java通信 ...

  6. flex+blazeds

    BlazeDS开发指南: http://www.cnblogs.com/xia520pi/archive/2012/05/26/2519343.html 使用BlazeDS实现Flex和Java通信 ...

  7. java多线程学习笔记——详细

    一.线程类  1.新建状态(New):新创建了一个线程对象.        2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...

  8. java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了)

    java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了)  目标效果(命令行启动服务): service xxxxd start #启动服务  servic ...

  9. Flex Socket与Java通信实例说明(转)

    Flex Socket与Java通信实例说明(转) 这两天一直在flex的Socket ,现在终于懂了很多.由浅到深一步一步深入.慢慢体会实例,虽然实例都是在网上找的,但也经过了我的测试.我比较喜欢注 ...

随机推荐

  1. 跟着未名学Office - 高效笔记OneNote

    了解OneNote 2016年12月22日    19:57 OneNote Summary 理解OneNote中的笔记本.分区.页的概念 编写人:未名 感谢秦大: http://www.zloffi ...

  2. DS二叉树--Huffman编码与解码

    题目描述 1.问题描述 给定n个字符及其对应的权值,构造Huffman树,并进行huffman编码和译(解)码. 构造Huffman树时,要求左子树根的权值小于.等于右子树根的权值. 进行Huffma ...

  3. 前端模拟后台返回数据之Mockjs

    一.官方文档: https://github.com/nuysoft/Mock/wiki/Syntax-Specification 例子:http://mockjs.com/examples.html ...

  4. 《Java并发编程实战》笔记-synchronized和ReentrantLock

    在一些内置锁无法满足需求的情况下,ReentrantLock可以作为一种高级工具.当震要一些高级功能时才应该使用ReentrantLock,这些功能包括:可定时的.可轮询的与可中断的锁获取操作,公平队 ...

  5. python之路——6

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 增dic['age'] = 21 dic.setfault()删popcleardel popitem ...

  6. python常用模块: random模块, time模块, sys模块, os模块, 序列化模块

    一. random模块  import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...

  7. [UE4]移动相机,使用Arrow组件来标记移动位置

    一.创建一个Arrow组件来标记要移动的位置(Arrow的用法之一就是用来标注坐标). 二.使用TimeLine时间轴结合插值Lerp来移动相机

  8. Redis进阶实践之十四 Redis-cli命令行工具使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...

  9. 各种波形文件VCD,VPD,SHM,FSDB生成的方法

    转载---http://www.cnblogs.com/zeushuang/archive/2012/11/14/2769640.html 仿真是IC设计不可或缺的重要步骤,仿真后一般需要记录下波形文 ...

  10. ping -c 3 localhost

    linux指令,ping -c count ip,向指定IP发送指定数量的ping包