Recipe 20.20 Returning Typed Objects from Java

20.20.1 Problem

You want to return a typed object from a J2EE back end.

20.20.2 Solution

Use the third-party ASTranslator class to convert a JavaBean object to an ASObject, and return the ASObject.

20.20.3 Discussion

For details on obtaining ASTranslator, see Recipe 20.17.

Once you have ASTranslator included in your web application, you can use the toActionScript( ) method of an ASTranslator object to convert a JavaBean object into an ASObject that can be returned to Flash. Here is an example of a Java method that creates a JavaBean object, converts it into an ASObject, and returns the ASObject:

public flashgateway.io.ASObject getTypedObject(  ) {   // Create a MyClass JavaBean object. See Recipe 20.14 for the MyClass code.   MyClass myCls = new MyClass(  );        // Set the properties of the object.   myCls.setA("eh");   myCls.setB("bee");   // Create an ASTranslator object.   com.carbonfive.flash.ASTranslator ast = new com.carbonfive.flash.ASTranslator(  );   // Convert the MyClass object to an ASObject.   flashgateway.io.ASObject aso = ast.toActionScript(myCls);   // Return the ASObject.   return aso; }

Here is an example ActionScript snippet that handles the returned ASObject:

function MyClass(a, b) {   this.a = a;   this.b = b; } MyClass.prototype.a; MyClass.prototype.b; // Add a method that just writes all the properties to the Output window. This // example illustrates that the ASObject is correctly converted into a MyClass object // when returned to Flash. MyClass.prototype.traceProperties = function (  ) {   for (var prop in this) {     trace(prop + " = " + this[prop];   } }; Object.registerClass("MyClass", MyClass); // Create a response object. myResponse = new Object(  ); myResponse.onResult = function (result) {   // The result parameter is correctly converted to a MyClass object. You can prove   // this because the traceProperties(  ) method is called.   result.traceProperties(  ); }; // Call the service function. myJ2EEService.getTypedObject(myResponse);

20.20.4 See Also

Recipe 20.14 and Recipe 20.17



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net