coldfusion_Coldfusion MX和Flash远程处理入门

coldfusion

One of the cool new features of Macromedia ColdFusion MX is Macromedia Flash Remoting. Macromedia Flash Remoting allows you to use ColdFusion MX functionality in your Macromedia Flash movies easily using familiar Macromedia Flash syntax and objects.

Macromedia ColdFusion MX的很酷的新功能之一是Macromedia Flash Remoting。 Macromedia Flash Remoting允许您使用熟悉的Macromedia Flash语法和对象轻松地在Macromedia Flash电影中使用ColdFusion MX功能。

Today, we’ll walk through a simple Macromedia Flash Remoting example. The SWF will use Macromedia Flash Remoting to call a ColdFusion MX component (also known as a CFC) on the server. The ColdFusion component will return a string, which will be loaded into Macromedia Flash and displayed.

今天,我们将看一个简单的Macromedia Flash Remoting示例。 SWF将使用Macromedia Flash Remoting调用服务器上的ColdFusion MX组件(也称为CFC)。 ColdFusion组件将返回一个字符串,该字符串将被加载到Macromedia Flash中并显示。

入门 (Getting Started)

1. Download the tutorial files.

1.下载教程文件。

All the files you’ll need can be downloaded here (35 KB).

您需要的所有文件都可以在此处下载(35 KB)

Create a folder at cf_webrootcommacromediatest. Unpack the ZIP file into the test folder. Note that throughout the tutorial, we’ll refer to cf_webroot Your cf_webroot varies based on your installation location.

在cf_webrootcommacromediatest创建一个文件夹。 将ZIP文件解压缩到测试文件夹中。 请注意,在整个教程中,我们将参考cf_webroot。cf_webroot随安装位置的不同而不同。

2. Install the required software

2.安装所需的软件

The software we’ll use through the tutorial includes:

我们将在本教程中使用的软件包括:

  • Macromedia Flash MX

    Macromedia Flash MX
  • Macromedia ColdFusion MX

    Macromedia ColdFusion MX
  • Flash Remoting Components for Macromedia Flash MX

    Macromedia Flash MX的Flash远程处理组件

You can download trial versions of the software listed here. Follow the installation wizards to ensure that all software is correctly installed.

您可以下载此处列出的软件的试用版。 请遵循安装向导,以确保正确安装了所有软件。

创建服务器端代码 (Creating the Server-side Code)

The server-side code is a simple ColdFusion component. It’s included in the file HelloWorld.cfc in the com/macromedia/test folder, and note that the file name HelloWorld.cfc also determines the service name for the component. So in this case, the service name is HelloWorld. If you open HelloWorld.cfc in a text editor, you’ll see this:

服务器端代码是一个简单的ColdFusion组件。 它包含在com / macromedia / test文件夹中的文件HelloWorld.cfc中,请注意,文件名HelloWorld.cfc还会确定组件的服务名称。 因此,在这种情况下,服务名称为HelloWorld。 如果在文本编辑器中打开HelloWorld.cfc,您将看到以下内容:

<cfcomponent name="HelloWorld">
 <cffunction name="sayHello" access="remote">
   <cfreturn "Hello World">
 </cffunction>
</cfcomponent>

As you can see, it’s pretty simple to create a basic component – we simply use the cfcomponent tag. Although the component represents a Macromedia Flash Remoting service, it does nothing by itself.

如您所见,创建一个基本组件非常简单-我们只需使用cfcomponent标签。 尽管该组件代表Macromedia Flash Remoting服务,但它本身不执行任何操作。

In order to make the component function, we add the cffunction tag — which is a method — to it. Thus, the component now contains a cffunction tag named "sayHello," in which the cfreturn tag is nested. And when you call this component, it returns a "HelloWorld" string to whichever URL or service etc. called the method. You can have as many functions as you’d like inside a component, but for this example, we’ll include just the one.

为了使组件发挥作用,我们向其添加了cffunction标记(这是一种方法)。 因此,组件现在包含一个cffunction指定的标记“的sayHello,”,其中cfreturn标签嵌套。 并且,当您调用此组件时,它将向任何称为该方法的URL或服务等返回“ HelloWorld”字符串。 您可以在一个组件内拥有任意数量的功能,但是在此示例中,我们将仅包含其中一个。

Notice also that the access attribute value is "remote" within the cffunction tag. This attribute is required if you want Flash movies (or other remote services) to access the component methods.

还要注意,cffunction标记中的访问属性值为“ remote ”。 如果要Flash电影(或其他远程服务)访问组件方法,则需要此属性。

And that’s it! We’ve successfully created a component. Note that the corresponding file is located here coldfusionmx_webrootcommacromediatestHelloWorld.cfc

就是这样! 我们已经成功创建了一个组件。 请注意,相应的文件位于此处coldfusionmx_webrootcommacromediatestHelloWorld.cfc

Why did I instruct you to place the files in that folder? Well, components aren’t referred to just by name – they’re also referred to by location (in this case, coldfusionmx_webrootcommacromediatest). So by placing your component in the commacromediatest folder, you:

我为什么指示您将文件放在该文件夹中? 好吧,组件不仅是通过名称来引用的,而是通过位置来引用的(在本例中为Coldfusionmx_webrootcommacromediatest)。 因此,通过将组件放置在commacromediatest文件夹中,您可以:

  1. Ensure that another developer on your team doesn’t overwrite your “HelloWorld" component with a "HelloWorld" component of his or her own.

    确保团队中的其他开发人员不要用自己的“ HelloWorld”组件覆盖您的“ HelloWorld”组件。
  2. adopt best practice organization of your components.

    采用组件的最佳实践组织。

And finally, you’ve probably noticed by now that com/macromedia is simply macromedia.com in reverse. Likewise, you should use your own domain name for the components that you create, such as net/foo if your site’s called foo.net.

最后,您现在可能已经注意到com / macromedia相反就是macromedia.com。 同样,对于创建的组件,应使用自己的域名,例如net / foo(如果您的站点称为foo.net)。

So, what’s next? At this point you could start building Macromedia Flash code to access your component, but first, let’s make a simple ColdFusion page to ensure that the component actually works. If we test it now, it’ll be easier to debug any problems that may arise than it would be to debug once we’d started working with Macromedia Flash MX.

下一个是什么? 此时,您可以开始构建Macromedia Flash代码来访问您的组件,但是首先,让我们创建一个简单的ColdFusion页面,以确保该组件能够正常工作。 如果我们现在对其进行测试,则调试可能出现的任何问题比开始使用Macromedia Flash MX进行调试要容易得多。

In the cfusionmx_webroot/com/macromedia/test/ directory, there is a file named test.cfm which contains the following code:

在cfusionmx_webroot / com / macromedia / test /目录中,有一个名为test.cfm的文件,其中包含以下代码:

<cfoutput> 
 <cfinvoke  
         component="com.macromedia.test.HelloWorld"  
         method="sayHello"  
     returnVariable="message"  
         />
     #message#
</cfoutput>

The file test.cfm uses the cfinvoke tag to call the component and then print the component's output with the cfoutput tag. Let's now take a quick look at the cfinvoke tag's attributes to understand what's going on.

The code which reads:

specifies which component will be called. The path, com.macromedia.test.HelloWorld points to the commacromediatestHelloWorld file relative to cf_webroot folder.

The following code:

method="sayHello"

specifies the function or method that is to be executed within the component.

The next section:

specifies the variable name we'll use to store any data returned from the component. We'll use this variable to return the string from the component method, and the cfoutput tag to display the variable in the Web browser.

Save the ColdFusion page, test.cfm, and view it in a Web browser, at http://localhost/com/macromedia/test/test.cfm (change the domain name, or add a port number as necessary for your particular set up). The message "Hello World" should display. If it doesn't, or you receive an error, check your component code, the CFML test page, and the URL for typos.

You've just experienced the power of components! They allow you to create modular code that can be used in many ways, and even by multiple programs (in this case, ColdFusion MX and Macromedia Flash MX). Now you're ready to connect to your component with Macromedia Flash MX!

Creating the Client Side Code in Macromedia Flash MX

Create a new Macromedia Flash movie, and then open it up with Macromedia Flash MX. In the folder where you unpacked the ZIP file for this tutorial, find the Macromedia Flash movie named gatewayHelloWorld.fla.

The Macromedia Flash movie we'll create will be simple: it will make a connection to the server, call the method on HelloWorld.cfc (which will load the data from the server), and display it in the output window. First, though, let's look at the ActionScript for the Macromedia Flash movie. Below is a step-by-step walkthrough of the code. The movie has one frame as shown below.

The ActionScript code is as follows: #include "NetServices.as"   #include "NetDebug.as"     function Result()   {    //receives data returned from the method    this.onResult = function(result)    {      trace("Data received from server : " + result);    }      this.onStatus = function(error)    {      trace("Error : " + error.description);    }   }     NetServices.setDefaultGatewayUrl   ("http://localhost:8500/flashservices/gateway");   var gw = NetServices.createGatewayConnection();   var server = gw.getService("com.macromedia.test.HelloWorld",   new Result());   server.sayHello();

First, the ActionScript libraries appear in the application code:

#include "NetServices.as"
#Include "NetDebug.as"

The ActionScript libraries are required, as they contain the ActionScript code and objects necessary to communicate with Macromedia Flash Remoting. NetDebug.as is used to debug the communication between Macromedia Flash and the server. When NetDebug.as is included, you can view all the client/server communication by opening the NetConnection panel within Macromedia Flash MX (Window > NetConnection Debugger).

Next, the movie creates a new ActionScript class, called Result. It uses the methods in an instance of the class to catch and manipulate the data received from the server.

This creates a Result class with two methods. The first method:

this.onResult = function(result)
{  
 trace("Data received from server : " + result);  
}

retrieves the response from the component on the server. Whenever data from the server loads, it calls the onResult method. In this case, the application code prints the response from the server to the output window in Macromedia Flash MX using the trace() method.

Next, the application code creates a function named onStatus. This is called if an error occurs when trying to load the data from the server. When an error occurs, it's passed to the method. You can access the error description through the description property, as is shown here. In this case, it prints the error message from the server to the output window in Macromedia Flash using the trace() method. The code below illustrates this scenario:

Note that the object that contains the methods does not need to be named Result.

Once you have created the Result class, you're ready to connect to the server. First, set the path to the ColdFusion MX server with the following code. If your ColdFusion MX server is running on a different port or domain name, just change the URL in this ActionScript accordingly.

NetServices.setDefaultGatewayUrl(http://localhost:8500/flashservices
/gateway);

Next, use the NetConnection object to connect to the ColdFusion MX server:

Note that the Macromedia Flash movie does not connect to the server at this point – instead, think of the object returned by this code as a connection to the server. Next, obtain a reference to the component that resides on the server you want to access:

var server = gw.getService("com.macromedia.test.HelloWorld",
new Result());

Notice that two parameters are passed to the getService method. The first parameter, com.macromedia.test.HelloWorld is the path to our component -- it defines the component that you're connecting to (you may need to change this parameter based on the location of your component).

The second parameter, new Result(), creates a new instance of the Result object. Passing the new Result () instance to the method indicates that the method should call the functions in that object (the onResult, and onStatus methods) whenever the server sends data or information.

It may seem odd that we're passing an instance to the Result class by passing a new Result(). We could have done the following instead:

However, as, in this case, we won't need to use the instance of the class once the data is loaded, we don't need to save an instance of it within a variable.

The getService() method returns an object that represents the actual component on the server. In this case, it is stored in a variable called server. Now, if you want to call a method in your component, call it through the server variable, as follows:

server.sayHello().

At this point, the code calls the service and component on the server. This is how it works:

  1. Flash MX calls the ColdFusion MX server, passing the component name to the server.
  2. Flash Remoting locates the component based on the path specified by Flash, and then calls the function within the component (also specified by Flash).
  3. The component function returns a string.
  4. The gateway receives the string, and passes it back to the Flash movie.
  5. Flash receives the string from the server (through Flash Remoting), and passes it to the onResult() method for the object specified (in this case, the Result class).

  6. The onResult method processes the data.

Running the Example

To run the example, and test the movie, use the following steps.

  1. Verify that the ColdFusion MX server is running.
  2. Within Flash MX, test the movie by going to Control > Test Movie. "Hello World" appears in the Flash MX output window, as follows:

    image

  3. Publish your Flash movie.
  4. View your Flash movie in a browser. The ColdFusion component produces the "Hello World" data in the Flash movie.
Conclusion

Now you know how to create a simple Flash movie that uses Flash MX, ColdFusion MX and Flash Remoting. Sure -- displaying "Hello World" may not seem that impressive or useful, however, remember that "Hello World" is simply a "string" object type. You can easily change your ColdFusion component so that it returns other types of data, such as arrays, structures, or database query results. Furthermore, you could use the following server-side code to populate your arrays, structures, or database queries:

  • ColdFusion Components (as in this example)

    ColdFusion组件(如本例所示)
  • ColdFusion pages

    ColdFusion页面
  • Server-side ActionScript

    服务器端ActionScript
  • Java

    Java
  • Web Services

    网页服务

These ColdFusion MX features help you leverage Macromedia MX functionality on your Website. These topics are featured in the Application Development Centers for ColdFusion MX, Macromedia Flash MX, and Dreamweaver MX.

这些ColdFusion MX功能可帮助您在网站上利用Macromedia MX功能。 这些主题在ColdFusion MX,Macromedia Flash MX和Dreamweaver MX的应用程序开发中心中提供。

Reproduced with permission from Macromedia

经Macromedia许可转载

翻译自: https://www.sitepoint.com/coldfusion-mx-flash-remoting/

coldfusion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值