Typically, BlazeDS is used to send typed objects between a Flex client and a Java server, but what if you want to send generic objects generated dynamically? On the client side, you can create an ActionScript object dynamically through the Object base class. So, for example, you can write something like this:
var myObject: Object = new Object(); myObject.prop1 = "prop1Value"; myObject.prop2 = new Object(); myObject.prop2.prop21 = "subValue1"; myObject.prop2.prop22 = "subValue2";
Just a simple example that shows how to dynamically add custom properties to a generic ActionScript Object. What could not be obvious is how to obtain the same functionality on the server side with Java. In fact, the Object class in ActionScript corresponds to the ASObject class in Java. What is important to note is that the ASObject class extends java.util.HashMap. How can this information be useful? To understand it, let’s immediately take a look at our example application.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
pageTitle="Sending ActionScript objects through BlazeDS with ASObject - devahead BLOG"
layout="vertical" horizontalAlign="left">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.AsyncToken;
protected static const ENDPOINT: String = "messagebroker/amf";
protected function getRemoteObject(): void
{
// Create a generic ActionScript object to send to the server
var sentGenericObj: Object = new Object();
sentGenericObj.prop1 = 123;
sentGenericObj.composite = new Object();
sentGenericObj.composite.sub1 = "subValue1";
sentGenericObj.composite.sub2 = 0.57;
var call: AsyncToken = testService.getSentGenericObject(sentGenericObj);
call.addResponder(new mx.rpc.Responder(
onGetRemoteObjectOK, onGetRemoteObjectFault));
}
protected function onGetRemoteObjectOK(event: ResultEvent): void
{
var result: Object = event.result;
Alert.show("Returned object:\n\n" +
"prop1 = " + result.prop1 + "\n" +
"composite.sub1 = " + result.composite.sub1 + "\n" +
"composite.sub2 = " + result.composite.sub2);
}
protected function onGetRemoteObjectFault(event: FaultEvent): void
{
Alert.show("Error retrieving remote object.\n\n" +
"faultString: " + event.fault.faultString + "\n" +
"faultDetail: " + event.fault.faultDetail + "\n" +
"faultCode: " + event.fault.faultCode, "Error");
}
]]>
</mx:Script>
<mx:RemoteObject id="testService" destination="testService" endpoint="{ENDPOINT}"
showBusyCursor="true"/>
<mx:Button label="Get remote object" click="{getRemoteObject()}"/>
</mx:Application>
In this application, we call a server function named getSentGenericObject. It’s purpose is simply to send back to the client an object generated dynamically on the server side whose values are the same of the object received in input from the client. As you can see, we create a generic ActionScript object called sentGenericObj, add some properties and send it to the server, then we print on an alert message the values of the object received from the server. Now it’s time to see how getSentGenericObject is implemented. Read the rest of this entry »

Recent Comments