`

flex BlazeDS

阅读更多

   技术了解:

   BlazeDS 是一个基于服务器的java 远程调用和web 消息传递技术。它能够使得运行在浏览器上的Flex 程序 和运行在web服务器上的Java 程序相互通信.

    前期工具:
    Tomcat  6
    JDK 1.6
    FlexBuilder4
    MyEclipse7.0
   下载BlazeDS   http://opensource.adobe.com/wiki/display/blazeds/BlazeDS  下载Turnkey 版 

    里面有Flex SDK ,Tomcat 和一些Demo 程序

 

    准备编码:

    1:在MyEclipse 下新建Web 工程 BlazeDS 

    2:减压blazeds-turnkey-4.0.1.21287.zip 下载文件

    3:减压blazeds.war 文件

    4:把 WEB-INF/lib 下的jar 文件拷贝到你的BlazeDS 工程的lib 下

    5:把WEB-INF 下的flex 文件夹拷贝到BlazeDS 工程的WEB-INF 文件夹下

    6:把WEB-INF 下的web.xml 文件替换BlazeDS 工程下的web.xml 

    7:编写自己的java 类

    package com.test;

public class HelloWorld {

	public String getHelloWorld(String name) {
		return "Hello World!" + name;

	}
}

    8:在BlazeDS 工程下的WebRoot/WEB-INF/flex 下找到remoting-config.xml 在其中添加如下配置服务代码:

      <destination id="helloWord">

<properties>

<source>com.test.HelloWorld</source>

</properties>

</destination>

 

   整个文件内容如下:

 

   <?xml version="1.0" encoding="UTF-8"?>

<service id="remoting-service"

class="flex.messaging.services.RemotingService">

        <adapters>

<adapter-definition id="java-object"

class="flex.messaging.services.remoting.adapters.JavaAdapter"

default="true" />

</adapters>

 

<default-channels>

<channel ref="my-amf" />

</default-channels>

 

<destination id="helloWord">

<properties>

<source>com.test.HelloWorld</source>

</properties>

</destination>

 

 </service>

 

   OK ,Java 服务端已近完工 接下来我们创建Flex 程序端

 

    1:用你的FlexBuilder 创建一个新工程 FlexDemo

    2:在FlexDemo.xml 文件中放置如下内容:

    <?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:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<mx:RemoteObject id="say" destination="helloWord">              
		</mx:RemoteObject>  
		
		<mx:RemoteObject id="service" destination="helloWord" fault="{failure(event);}" source="helloWord" showBusyCursor="true" concurrency="single" endpoint="{urlStr}">
			<mx:method name="getHelloWorld" result="{loadDataSuccess(event);}"  />  
		</mx:RemoteObject>
	</fx:Declarations>
	<mx:Button x="335" y="80" label="Click" click="remotingSayHello(event);"/>        
	<mx:TextInput x="159" y="80" id="tiName"/>    
	<mx:Label x="109" y="82" text="name:"/>  
	<mx:Label   x="44" y="162" width="448" height="71" id="lblView" color="#FCEE09" fontSize="20" fontWeight="bold" textDecoration="underline" fontStyle="normal" />   
			
    <fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
			private   var server:String = "http://127.0.0.1:8080/BlazeDS";
			private   var endpoint:String = "/messagebroker/amf";
			private var urlStr:String=server+endpoint;
			
			private function failure(info:Object):void{
				Alert.show("加载数据失败");
				return ;
			}
			 private function loadDataSuccess(event:ResultEvent):void{
				 this.lblView.text=event.result as String;
			 }
			
			
			public function remotingSayHello(event:Event):void{           
				var iname:String=tiName.text;             
				service.getHelloWorld(iname);             
			}   
		 ]]>
	</fx:Script>
	
</s:Application>
 

 

     Ok ,flex 程序 完成 ,在MyEclipse 下部署BlazeDS 应用程序 并启动tomcat  

     运行FlexDemo.xml  页面

 

     运行结果如下图:

     

 

 

 

 

 

http://translate.google.com.hk/translate?hl=zh-CN&langpair=en|zh-CN&u=http://www.adobe.com/devnet/flex/articles/spring_blazeds_integration.html

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics