整了很长时间的通信,还是一直有一点问题。现在搞定了,记录一下,也跟有需求的同学们共享。
我重新把所有的过程再做一遍。
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 写后台
packagecom;public classHelloWorld {publicString getString(String name){
System.out.println("daole");return "这里是后台-:-这里是前台"+name;
}
}
4写配置文件--修改WebRoot》WEB-INF》flex>remoting-config.xml
增加
com.HelloWorld
5写前台
protectedfunction resultHandler(event:ResultEvent):void{//TODO Auto-generated method stub
Alert.show("进入result");
Alert.show(event.result.toString(),"提示");
}
protectedfunction faultHandler(event:FaultEvent):void{//TODO Auto-generated method stub
Alert.show("进入faultHandler");
Alert.show(event.fault.toString(),"提示");
}
]]>
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,找到如下图路径的配置文件
问题来了,通道是默认的,都是变量,可以替换
{context.root}被替换成了WebRoot了呗。
先手动改了my-amf的通道路径看看能不能用。
这里要注意重新清理项目,否则配置文件仍然是默认的。
明显修改了路径是可以的。但这样修改似乎不太好。我们想办法修噶器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的删掉才行
试试看,
修改为
解决了。
7穿插解决一个小问题
由于“RemoteObject”声明未实现“mx.core.IUIComponent”,它必须包含在 标签中 怎么解决
把 RemoteObject 标签放到 标签中就可以了,如:
8增加一个能够输入参数的代码
前台
protectedfunction resultHandler(event:ResultEvent):void{//TODO Auto-generated method stub
Alert.show("进入result");
Alert.show(event.result.toString(),"提示");
}
protectedfunction faultHandler(event:FaultEvent):void{//TODO Auto-generated method stub
Alert.show("进入faultHandler");
Alert.show(event.fault.toString(),"提示");
}
protectedfunction remotingSayHello(event:Event):void{//TODO Auto-generated method stub
var iname:String =tiName.text;
say.getHelloWorld(iname);
Alert.show("1");
}
]]>
View Code
后台
packagecom;public classHelloWorld {publicString getString(String name){
System.out.println("daole");return "这里是后台-:-这里是前台"+name;
}publicString getHelloWorld(String name){
System.out.print("执行到了getHelloWorld这里");return "HelloWorld!"+name;
}
}
View Code
源代码分享一下吧:
http://yunpan.cn/QGzvBUYieXvVS 访问密码 20d7
请尊重作者劳动成果。