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 getStudent() {
		List 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在这个文件中添加:
    
        
            net.fleet.HelloWorld
        
    

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

二:客户端访问两种方式:
1,用标签访问


	
		
		
		
		
		
	
	
		
	
	
	  
		  
			  
			  
		  
	
	
	
	
	


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)