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

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

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. 用windows自带的ftp.exe实现断点续传的方法

    摘自http://www.jb51.net/article/10604.htm 动画下载地址: http://www.chinesehack.org/soft/book/goonftp-jc.rar ...

  2. Java NIO系列教程(四) Scatter/Gather

    Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作.分散(sc ...

  3. 学习笔记之Bokeh

    Welcome to Bokeh — Bokeh 0.12.16 documentation https://bokeh.pydata.org/en/latest/ Bokeh is an inter ...

  4. Linux 下 MQ 的安装

    在WebSphere MQ 7.1版本以前,同时只能有一个产品的安装实例,在UNIX和Linux系统上,/usr/lib,/usr/bin和/usr/include目录下会增加一些软连接,也指向了这个 ...

  5. JVM性能、多线程排查常用命令

    最近遇到很一个很棘手的多线程问题,跟踪了几天终于解决了,在此记录跟踪过程的常用命令,后期有空再做具体的事件总结.软件的开发一定要有监控,一定要有监控,一定要有监控,重要的事情说三遍.没有监控的软件就是 ...

  6. Webbrowser中IHTMLElement、IHTMLElement2、IHTMLDocument2、IHTMLDocument2属性介绍

    一.IHTMLElement接口        ele.click  -----------点击事件 ele.setAttribute(const strAttributeName: WideStri ...

  7. windows 查询文件被什么程序占用

    运行Resmon CPU选项卡全选 在[关联的句柄]里查询: 需要的时间挺多的...

  8. django练习题

    1.Web框架的本质是什么?为什么要有Web框架? 所有的Web应用,本质上其实就是一个socket服务端,用户端程序其实就是一个socket客户端.对于真实开发中的python web程序来说,一般 ...

  9. 把一串数字表示成千位分隔形式——JS正则表达式的应用

    梳理思路 要先明白的是,我们将要转换成的数字格式是这样:从个位往左数起,每三位前插入一个千位分隔符,,即可以想象成我们要把每三位数字前面的那个空""匹配出来,并替换成千位分隔符,. ...

  10. 《Linux 性能及调优指南》2.3 监控工具

    翻译:飞哥 (http://hi.baidu.com/imlidapeng) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance a ...