做个备忘。
Flex中的Feature.as映射自Java中的Feature.java 。
Flex中的Feature类需要绑定Java中的Feature类。
package hashmap { [Bindable] [RemoteClass(alias="hashmap.Feature")] public class Feature { public var FeatureID:String; public var FeatureName:String; public var FeatureImageURL:String; public var FeatureWidth:String; public var FeatureHeight:String; public function Feature(){ } // public function Feature(ID:String, Name:String, ImageURL:String, Width:String, Height:String){ // this.FeatureID = ID; // this.FeatureName = Name; // this.FeatureImageURL = ImageURL; // this.FeatureWidth = Width; // this.FeatureHeight = Height; // } public function getFeatureID():String{ return FeatureID; } public function setFeatureID(featureID:String):void{ this.FeatureID = featureID; } public function getFeatureName():String{ return FeatureName; } public function setFeatureName(featureName:String):void{ this.FeatureName = featureName; } public function getFeatureImageURL():String{ return FeatureImageURL; } public function setFeatureImageURL(featureImageURL:String):void{ this.FeatureImageURL = featureImageURL; } public function getFeatureWidth():String{ return FeatureWidth; } public function setFeatureWidth(featureWidth:String):void{ this.FeatureWidth = featureWidth; } public function getFeatureHeight():String{ return FeatureHeight; } public function setFeatureHeight(featureHeight:String):void{ this.FeatureHeight = featureHeight; } } }
Java中的Feature类需要实现java.io.Serializable
package hashmap; public class Feature implements java.io.Serializable { public String FeatureID; public String FeatureName; public String FeatureImageURL; public String FeatureWidth; public String FeatureHeight; public Feature(){ } public Feature(String ID, String Name, String ImageURL, String Width, String Height){ this.FeatureID = ID; this.FeatureName = Name; this.FeatureImageURL = ImageURL; this.FeatureWidth = Width; this.FeatureHeight = Height; } public String getFeatureID() { return FeatureID; } public void setFeatureID(String featureID) { FeatureID = featureID; } public String getFeatureName() { return FeatureName; } public void setFeatureName(String featureName) { FeatureName = featureName; } public String getFeatureImageURL() { return FeatureImageURL; } public void setFeatureImageURL(String featureImageURL) { FeatureImageURL = featureImageURL; } public String getFeatureWidth() { return FeatureWidth; } public void setFeatureWidth(String featureWidth) { FeatureWidth = featureWidth; } public String getFeatureHeight() { return FeatureHeight; } public void setFeatureHeight(String featureHeight) { FeatureHeight = featureHeight; } }
Java中的方法,返回HashMap:
package hashmap; import java.util.*; public class TestHashMap { public HashMap<String, List<Feature>> getHashMap(){ ArrayList<Feature> shapeList=new ArrayList<Feature>(); Feature shape1 = new Feature("1","AAA","URL1","",""); Feature shape2 = new Feature("2","AAA","URL2","",""); Feature shape3 = new Feature("4","AAA","URL3","",""); shapeList.add(shape1); shapeList.add(shape2); shapeList.add(shape3); ArrayList<Feature> sizeList=new ArrayList<Feature>(); Feature size1 = new Feature("5","AAA","","1.3","2.1"); Feature size2 = new Feature("7","AAA","","2.5","3.0"); Feature size3 = new Feature("11","AAA","","3.3","3.8"); Feature size4 = new Feature("24","AAA","","3.3","3.8"); sizeList.add(size1); sizeList.add(size2); sizeList.add(size3); sizeList.add(size4); ArrayList<Feature> MaterialList=new ArrayList<Feature>(); Feature Material1 = new Feature("43","AAA","","",""); Feature Material2 = new Feature("44","AAA","","",""); Feature Material3 = new Feature("57","AAA","","",""); MaterialList.add(Material1); MaterialList.add(Material2); MaterialList.add(Material3); HashMap<String,List<Feature>> map=new HashMap<String,List<Feature>>(); map.put("Shape", shapeList); map.put("Size", sizeList); map.put("Material", MaterialList); return map; } }
Flex中的index.mxml :
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Button click="getHashMap()" label="GetHashMap" x="43" y="134"/> <mx:List id="lst" dataProvider="{acShape}" labelField="FeatureName" x="10" y="164"/> <!--<mx:DataGrid id="dg" dataProvider="{acShape}" x="12" y="167"/>--> <mx:RemoteObject id="roHashMap" destination="testHashMap" result="resultHashMap(event)" fault="faultHashMap(event)"/> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.controls.Alert; import hashmap.Feature; private var sResultHashMap:String; [Bindable] private var obj:Object = new Object(); [Bindable] private var acShape:ArrayCollection = new ArrayCollection(); private function getHashMap():void{ roHashMap.getHashMap(); } private function resultHashMap(event:ResultEvent):void{ acShape.removeAll(); obj = event.result as Object; for each(var fea:Feature in obj.Shape){ acShape.addItem(fea); trace(fea.FeatureName); } } private function faultHashMap(event:FaultEvent):void{ sResultHashMap = event.fault.message.toString(); Alert.show("error:\n" + sResultHashMap); } ]]> </mx:Script> </mx:Application>
WebRoot\WEB-INF\flex\remoting-config.xml 中添加:
<destination id="testHashMap"> <properties> <source>hashmap.TestHashMap</source> </properties> </destination>