Flex + LCDS + J2EE Web 项目(3) 传值

请关注新浪微博 账号:大数据进行时(http://weibo.com/u/3272022684

传递String类型到J2EE,以及接受来自于J2EE的返回值String类型

传递Object类型到J2EE,以及接受来自于J2EE的返回值Object类型

传递Array类型到J2EE,以及接受来自于J2EE的返回值List类型

接自 Flex + LCDS + Java 的Web 项目(2)  HelloWorld
http://quasimodo-es.iteye.com/blog/503084

.mxml文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	
	<mx:RemoteObject id="MyRO" destination="HelloWorldRO"/>
	
	
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
		
			//hello world 测试通信
			private function onClick() : void{
				MyRO.sayHello("  java ");
			}
			
			
			//向JAVA端传递String类型的值 
			private function onStringClick() : void{
				var strArgs : String = " hello java =========== ";//
				MyRO.addEventListener(ResultEvent.RESULT,handleStringResult);
				MyRO.getFlexString(strArgs);//调用J2EE的方法
				
			}
			//接受返回的String
			private function handleStringResult(event : ResultEvent) : void{
				Alert.show(event.result.toString());
			}
			
			
			//传递Object
			private function onObjectClick() : void{
				var obj : User = new User();
				obj.username = " flex username === ";
				obj.password = " flex password === ";
				MyRO.addEventListener(ResultEvent.RESULT,handleObjectResult);
				MyRO.getFlexObject(obj);
			}
			//接受返回的Object
			private function handleObjectResult(event : ResultEvent) : void{
				var obj : User = event.result as User;
				Alert.show(obj.username + "\n" + obj.password);
			}
			
			//传递Array
			private function onArrayClick() : void{
				var arr : Array = new Array();
				for(var i : int = 0 ; i < 4 ; i++){
					var obj : User = new User();
					obj.username = " flex username === " + i;
					obj.password = " flex password === " + i;
					arr.push(obj);
				}
				
				MyRO.addEventListener(ResultEvent.RESULT,handleArrayResult);
				MyRO.getFlexArray(arr);
				
			}
			//接受返回的List
			private function handleArrayResult(event : ResultEvent) : void{
				var ac : ArrayCollection = event.result as ArrayCollection;
				for(var i : int = 0 ; i < ac.length ; i++){
					var user : User = ac.getItemAt(i) as User;
					Alert.show(user.username + " \n " + user.password);
				}
			}
						
			
		]]>
	</mx:Script>
	
	<mx:Button x="52" y="29" label="hello java" click="onClick()"/>
	<mx:Button x="52" y="119" label="String" click="onStringClick()"/>
	<mx:Button x="52" y="225" label="Object" click="onObjectClick()"/>
	<mx:Button x="52" y="328" label="Array" click="onArrayClick()"/>
	
</mx:Application>



Flex 端的 User类
package
{
	[Bindable]
	[RemoteClass(alias='com.User')]//序列化,对应于J2EE的User类,User类中必须有默认构造方法
	public class User
	{
		public var username : String;
		public var password : String; 
		public function User()
		{
		}

	}
}


J2EE和Flex通信的接口:

package com;

import java.util.ArrayList;
import java.util.List;

import flex.messaging.io.amf.ASObject;

public class Hello {
	
//	方法必须是public 
	public void sayHello(String str){
		System.out.println("   hello world , and hello  " + str);
	}
	
	//接受从Flex 传来的参数,并返回一个String参数
	public String getFlexString(String str){
		
		System.out.println(str);
		
		return " hello Flex ==============  ";
		
	}
	
	//接受从Flex 传来的参数,并返回一个User参数
	public User getFlexObject(User user){
		System.out.println(user.getUsername() + "\n" + user.getPassword());
		User userVO = new User();
		userVO.setUsername(" java username == ");
		userVO.setPassword(" java password ==");
		return userVO;
	}
	
	//接受从Flex 传来的参数Array,并返回一个List参数
	public List getFlexArray(List list){
		
		List returnList = new ArrayList<User>();
		
		for(int i = 0 ; i < list.size() ; i++){
			
			User user = (User) list.get(i);
			
			System.out.println(user.getUsername() + "\n" + user.getPassword());
			
			User returnUser = new User();
			
			returnUser.setUsername(" java username " + i);
			returnUser.setPassword(" java password " + i);
			returnList.add(returnUser);
		}
		return returnList;
	}
	
}



Java的User类
package com;

public class User {
	private String username;

	private String password;

	public User() {
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}





项目源码下载: http://download.csdn.net/source/1780264

你可能感兴趣的:(java,Flex,blazeds,lcds)