主题:  通过CF组件网页传递数据

wait

职务:普通成员
等级:1
金币:10.0
发贴:220
#12003/1/20 14:37:32
转摘自:http://www.zdnet.com.cn

如今的Flash 已经不仅仅是一个UI(user interface)的设计工具,随着Flash Remoting的出现,她已经成为 Web application 开发工具的一种。它可以通过与后台程序的配合,可以实现所有传统应用服务器的功能,同时极大的提高客户体验。Macromedia 把这种应用称为 Rich Internet Application。

说到Flash与后台的结合,当然首选其自家兄弟产品 ColdFusion MX。首先,下载ColdFusion MX 和 Flash Remoting 组件。然后,安装这两个东东。具体的安装方法小弟我就不再多说了。好了,

下面开始动手!
首先,打开你的Flash MX。点击 File —— New From Template 。然后选择 web ——basic。在我们正确安装Flash Remoting 组件以后,就可以看到这个模版。

模版建好后它会自动的在第一帧产生一些代码,通过这些代码,我们就可以和后台传递数据了。其中,重要的是这一段。

if (inited == null)
{
// do this code only once
inited = true;
// set the default gateway URL (this is used only in authoring)
NetServices.setDefaultGatewayUrl(_
"http://localhost:8100/flashservices/gateway");
// connect to the gateway
gateway_conn = NetServices.createGatewayConnection();
// get a reference to a service
myService = gateway_conn.getService("my.service",this);
}
这一段初始化了连接服务,我们将 myService = gateway_conn.getService("my.service", this); 改为 myService = gateway_conn.getService("Remote.message", this); "Remote.message" 是我们即将要定义的 CF 组件地址。组件位于C:\CFusionMX\wwwroot\Remote 这个目录下。



请参考上图。左边的是 Input Text 命名为 Input_txt;右边的是 Dynamic Text 命名为 Output_txt;下面的按钮是一个组件,在属性面板中,把它的 Label 设置为显示,Click Handler 设置为 doDisplay。Click Handler 就是动作发生事出发的事件名称。
好了,界面我们设计完毕。目的就是,在输入文本框输入文字后点击按钮,让输出文本框显示同样的文字,而其中的传递是由后台的CF 组件完成的。

接下来,我们添加 ActionScript ,完整的代码如下:


#include "NetDebug.as"
#include "NetServices.as"
function doDisplay() {
myService.getMsg(Input_txt.text);
}
function getMsg_Result(result) {
Output_txt.text = result;
}
if (inited == null) {
inited = true;
NetServices.setDefaultGatewayUrl_
("http://localhost:8500/flashservices/gateway");
gateway_conn = NetServices.createGatewayConnection();
myService = gateway_conn.getService("Remote.message", this);
}
stop();
我们看看里面的两个函数:


function doDisplay() {
myService.getMsg(Input_txt.text);
}
这个函数就是当按下“显示”按钮时,向CF 组件中的一个函数传递参数。在这个例子里,函数名称是“getMsg”,参数就是 Input_txt 文本框中的文字。


function getMsg_Result(result) {
Output_txt.text = result;
}
这个函数就是当“getMsg”返回值的时候,将返回的值显示在 Output_txt 文本框里。

OK! Flash 部分的设计和编码工作已经完成。接下来,我们编写 CF 组件。非常简单,用写字板编写就行了。
完整的代码如下:








大家可以看到 ColdFusion 语句和HTML 很类似。 标签说明其中的代码属于组件。接着,定义了一个函数 ,这个函数的名称是 "getMsg" ,访问方法属性是"remote",CF 与 Flash 通讯都是"remote",返回类型属性是"any"。然后,我们定义了一个参数,,这个参数就是从 Flash 中得到的。在定义各个变量 “msg”,并将刚才参数的值附给它。最后我们将msg的值作为函数的返回值。

好!组件的编码工作完成了。将它保存在 C:\CFusionMX\wwwroot\Remote\ 目录下,注意命名为 message.cfc ,CFC就是ColdFusion 组件文件的后缀。
大功告成!测试吧!这个例子非常简单,但是大家可以根据它来举一反三,做出更复杂的应用。下次,我再和大家分享ColdFusion MX与Flash MX互动的其他技术。



s22

职务:版主
等级:4
金币:10.0
发贴:1634
#22003/1/21 12:37:14
if (inited == null)
{
// do this code only once
inited = true;
// set the default gateway URL (this is used only in authoring)
NetServices.setDefaultGatewayUrl(_
"http://localhost:8100/flashservices/gateway");
// connect to the gateway
gateway_conn = NetServices.createGatewayConnection();
// get a reference to a service
myService = gateway_conn.getService("my.service",this);
}
======
两段代码是一样的。
if (inited == null)
{
inited = true;
gateway_conn = NetServices.createGatewayConnection("http://localhost:8100/flashservices/gateway");
myService = gateway_conn.getService("my.service",this);
}



金山羊

职务:普通成员
等级:4
金币:10.0
发贴:827
#32003/1/21 12:41:06
flash与同门jrun的结合同样优秀
不过coldsion可以让你不会xml而提供webservice


5d.cn
FLASHDEP
TECHNOLOGY SUPPORT
JAVA/ACTIONSCRIPT/javascript
CFML/JSP/COLDFUSION/FLASH REMOTING
JRUN/COLDFUSIONMX
FLASH/DREAMWEARER