Flex+BlazeDS+java通信详细笔记
整了很长时间的通信,还是一直有一点问题。现在搞定了,记录一下,也跟有需求的同学们共享。
我重新把所有的过程再做一遍。
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通信详细笔记的更多相关文章
- Flex+BlazeDS+java通信详细笔记2-推送
前台是Air,后台是java 在运行之前,先要在IE地址栏输入http://127.0.0.1:8080/PushDemo/TickCacheServlet?cmd=start 激活它. 地址:htt ...
- Flex和Java通信报错
1.错误描述 11-30 18:15:52 ERROR [localhost-startStop-1] org.springframework.web.servlet.FrameworkServlet ...
- Flex与Java通信之HttpService
flashbuilder4.6.myeclipse10 参考:http://www.cnblogs.com/lovemoon714/archive/2012/05/25/2517684.html 1. ...
- java 多线程详细笔记(原理理解到全部使用)
鸽了好久以后终于又更新了,看同学去实习都是先学源码然后修改之类,才发觉只是知道语法怎么用还远远不够,必须要深入理解以后不管是学习还是工作,才能举一反三,快速掌握. 目录 基础知识 进程与线程 线程原子 ...
- Flex与Java交互(Flex调用java类展示数据)解析xml展示数据
Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869 Flex与java通信 ...
- flex+blazeds
BlazeDS开发指南: http://www.cnblogs.com/xia520pi/archive/2012/05/26/2519343.html 使用BlazeDS实现Flex和Java通信 ...
- java多线程学习笔记——详细
一.线程类 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...
- java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了)
java编写service详细笔记 - centos7.2实战笔记(windows类似就不在重复了) 目标效果(命令行启动服务): service xxxxd start #启动服务 servic ...
- Flex Socket与Java通信实例说明(转)
Flex Socket与Java通信实例说明(转) 这两天一直在flex的Socket ,现在终于懂了很多.由浅到深一步一步深入.慢慢体会实例,虽然实例都是在网上找的,但也经过了我的测试.我比较喜欢注 ...
随机推荐
- 黄聪:AngularJS中的$resource使用与Restful资源交互(转)
原文:http://blog.csdn.net/he90227/article/details/50525836 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以 ...
- Django--ORM--模型增删改查--备忘
以上运算符都区分大小写,在这些运算符前加上i表示不区分大小写,如iexact.icontains.istartswith.iendswith.insert into tb_bookinfo()valu ...
- 【springboot】之将properties配置转为bean
将springboot里面非application.yml 或者application.properties 里面的key-value转为JavaBean /** * @Describe: DataS ...
- 【linux】之内核升级
安装docker要满足一定的条件,对于cents系统,要求必须是64位,并且内核版本是3.10以上. 如果你的centos操作系统内核低于3.10,需要升级到这个版本以上,才能安装docker. 第一 ...
- 关于linux中的system函数
Linux下使用system()函数一定要谨慎 https://blog.csdn.net/senen_wakk/article/details/51496322 system()正确应用 https ...
- jython笔记
这篇笔记主要记录了我使用jython的一些问题点: 首先,jython是一个Java写的用来解析python语言的工具,他可以做到运行环境中没有python也可以使用python. jython采用的 ...
- BOS物流项目心得
定区管理 (和分区有何区别) : 区域管理针对自然行政区, 行政区域比较大,不可能让取派员去负责整个行政区域, 需要进行分区,将行政区域细分 ,成为很多小区域(分区), 需要为分区知道取派人员 , 在 ...
- Linux下设置固定IP的方法
本文转自http://blog.163.com/liulina_517@126/blog/static/3766198320118231431594/ linux系统安装完,以后通过命令模式配置网卡I ...
- Jmeter(七)Jmeter脚本优化(数据与脚本分离)
午休时间再来记一记,嗯..回顾着使用Jmeter的历程,想着日常都会用到的一些功能.一些组件:敲定了本篇的主题----------是的.脚本优化. 说起脚本优化,为什么要优化?又怎么优化?是个永恒的话 ...
- [UE4]在AIController中使用行为树
行为树会在Root根下面的每个子节点中从左右到右来回往复循环执行.