flex 之RemoteObject + blazeds 与java服务端通信

一,blazeds 服务端搭建
1)在eclipse中创建web project
2)下载blazeds.zip
3)把解压后的WEB-INF 和 META-INF 覆盖创建的web project
4)在web project中创建一个远程处理的java 类如:
package net.fleet;
import java.util.List;
import flex.messaging.io.ArrayList;

public class HelloWorld {
	public String say(String name) {
		System.out.println("funck you fuck you");
		return "hello " + name;
	}

	public List<Student> getStudent() {
		List<Student> students = new ArrayList();

		Student student = new Student("chenchaoyang", "[email protected]");
		Student student2 = new Student("chenchaoyang", "[email protected]");

		students.add(student);
		students.add(student2);

		return students;
	}
}

Studetn类就不再次赘述了。
5)配置远程处理java类
WebRoot\WEB-INF\flex\remoting-config.xml在这个文件中添加:
    <destination id="product" channels="my-amf">
        <properties>
            <source>net.fleet.HelloWorld</source>
        </properties>
    </destination>

6)部署项目到tomcat,启动tomcat。至此服务端已经配置完毕。

二:客户端访问两种方式:
1,用标签访问
<?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>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		<s:RemoteObject id="server" destination="product" result="server_resultHandler(event)" fault="server_faultHandler(event)"/>
		
		<s:RemoteObject id="server2" destination="product" result="getstudent_resultHandler(event)" fault="getstudent_faultHandler(event)"/>
		
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			protected function server_resultHandler(event:ResultEvent):void
			{
			  Alert.show("远程调用成功。。。。" + event.result,"提示");
			}
			
			protected function server_faultHandler(event:FaultEvent):void
			{
			  Alert.show("远程调用失败。。。。" + event.fault.toString(),"提示");
			}
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
			  server.say("chenchaoyang!");
			}
			
			protected function getstudent_resultHandler(event:ResultEvent):void
			{
				Alert.show("远程调用成功。。。。" + event.result,"提示");
				
				dg.dataProvider = event.result;
			}
			
			protected function getstudent_faultHandler(event:FaultEvent):void
			{
				Alert.show("远程调用失败。。。。" + event.fault.toString(),"提示");
			}
			protected function getstudent_clickHandler(event:MouseEvent):void
			{
				server2.getStudent();
			}
		]]>
	</fx:Script>
	
	<mx:DataGrid y="156" width="674" height="357" horizontalCenter="0" id="dg">  
		<mx:columns>  
			<mx:DataGridColumn headerText="ID" dataField="name"/>  
			<mx:DataGridColumn headerText="Name" dataField="email"/>  
		</mx:columns>  
	</mx:DataGrid>
	
	<s:Button x="390" y="198" width="214" height="93" label="按钮" click="button1_clickHandler(event)"/>
	
	<s:Button x="490" y="298" width="214" height="93" label="获取学生列表" click="getstudent_clickHandler(event)"/>
</s:Application>


2,手动创建remoteObject对象,并设置其相关属性访问
package com.superwulei.model
{
	import com.superwulei.model.vo.UserVO;
	import mx.collections.ArrayCollection;
	import mx.rpc.remoting.RemoteObject;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.ObjectProxy;

	public class UserProxy extends Proxy
	{
		private static var channel:Channel;
		
		public static const NAME:String = 'UserProxy';
		
		public function UserProxy()
		{
			super(NAME,new ArrayCollection());
		}
		
		public function get users():ArrayCollection{
			return data as ArrayCollection;
		}
		
		/* 添加项 */
		public function addItem(item:Object):void{
			
			if(channel == null)
			{
				channel = new AMFChannel("my-amf", "http://localhost:8080/111/messagebroker/amf");
			}
			var service:RemoteObject = new RemoteObject();
			var cs:ChannelSet = new ChannelSet();
			cs.addChannel(channel);
			service.channelSet = cs;
			service.destination = "product";
			service.addEventListener(FaultEvent.FAULT,showError);
			
			service.say("chenchaoyang!");
			
			users.addItem(item);
		}
		
		/* 删除项 */
		public function deleteItem(item:Object):void{
			var user:UserVO = item as UserVO;
			for(var i:int = 0; i < users.length; i++){
				if(users[i].email == user.email){
					users.removeItemAt(i);
				}
			}
		}
		
		private static function showError(e:FaultEvent):void {
			Alert.show(String(e.fault.message), "错误");
		}
	}
}



你可能感兴趣的:(Flex)