Using BlazeDS to Enable Flex to Send Data To A Java Class
Introduction
This blog entry continues my exploration of how to use Flex, Java, and BlazeDS together. In this tutorial I explain how to send data from Flex to a Java class.
Setup This Example
Now that I've got BlazeDS providing the plumbing on my server so the my Flex application can communicate with the backend Java classes, it's time to start testing how to use Java on the backend instead of my previous (and beloved) ColdFusion 8. If you've not yet read the first tutorial on setting up BlazeDS with Flex and Java, then check it out before trying this example.
This example will continue the simple HelloWorld from the previous tutorial. The goal with this iteration is to send a String value from Flex to the Java class method that will cause the method to return a custom String message.
You can view a demo of this example here.
Here is the Java class:
package example;
public class HelloWorldName {
public HelloWorldName() {
}
public String getHelloByName(String aName){
return "Hello from Java, " + aName + "!";
}
}
Note that the getHelloByName method must receive a String value for its parameter. Compile this Java class and place the resulting class file in [tomcat-home]\webapps\blazeds\WEB-INF\classes\example (since the class package statement is package example, the compiled class should go in the example folder under classes).
Add the following destination node to the remote-config.xml file.
<destination id="helloworldname">
<properties>
<source>example.HelloWorldName</source>
</properties>
<adapter ref="java-object"/>
</destination>
Stop Tomcat if its running and then restart Tomcat.
Create a new Flex project following the guidelines given in the first tutorial. Below is the code that goes between the mx:Application tags.
<mx:Script>
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.utils.StringUtil;
/*
Called when sayHelloBtn is clicked
*/
private function processSayHello():void {
ro.getHelloByName( names.value );
}//end function processLogin
private function resultHandler(event:ResultEvent):void
{
//used for debugging - shows details about result
//returned by the Java class method
Alert.show( ObjectUtil.toString(event.result) );
}
private function faultHandler(event:FaultEvent):void
{
Alert.show( ObjectUtil.toString(event.fault) );
}
</mx:Script>
<!--note the compiled java class HelloWorldName should be in a folder
named example and under the blazeds\WEB-INF\classes
folder which is in webapps-->
<mx:RemoteObject id="ro" destination="helloworldname"
result="resultHandler(event)"
fault="faultHandler(event)"/>
<mx:Panel x="10" y="10" width="440" height="200" layout="vertical" title="Test Flex 3 Using Java" borderColor="#008040" fontFamily="Arial" fontWeight="bold" fontSize="13">
<mx:Text text="Select a name to say hello to and then click the button" fontWeight="bold"/>
<mx:Spacer height="10"/>
<mx:ComboBox id="names" width="250">
<mx:ArrayCollection>
<mx:Array>
<mx:String>Bruce</mx:String>
<mx:String>Kate</mx:String>
<mx:String>Andrew</mx:String>
</mx:Array>
</mx:ArrayCollection>
</mx:ComboBox>
<mx:Spacer height="10"/>
<mx:Button label="Say Hello!" id="sayHelloBtn" click="processSayHello()"/>
</mx:Panel>
When the user clicks on the button, ActionScript function processSayHello is called. In this function the program tells the ro (id value for the mx:RemoteObject) to call the getHelloByName method and sends that method the String value of the selected name in the names ComboBox.
If the result from the calling the Java class is returned successfully, then ActionScript method resultHandler() is called. An alert box is displayed which contains the String returned by the HelloWorldName Java class method getHelloByName.
Important Points
Note one thing about the mx:RemoteObject tag. This time I did not include the source attribute. From what I've read about BlazeDS, the source attribute is not necessary in the mx:RemoteObject tag if the destination value has been specified in the Remote-config.xml file. If you examine the remote-config.xml file addition (see above) I specified a destination node with an id value of helloworldname. This node contains a child node of source that provides the name of the Java class file to call upon.
One other thing to note about the Java class file. According to the BlazeDS documentation, the class file must have a default constructor (a constructor with no parameters).
With BlazeDS providing the means to integrate Flex and Java, using mx:RemoteObject to communicate with a Java class seems to be just like how I used mx:RemoteObject to communicate with ColdFusion Components (CFCs). I hope that will continue to be true as I explore more complicated Flex-Java integration examples.
What's Next
Next I'll work up an example of mapping an ActionScript 3.0 class to a Java class so that Flex and Java can exchange user-defined data types (such as a Person object or a collection of Person objects).
Really awesome! This article helped me in making BlazeDS more clear.
Thanks,
Rahul Sahay
A really goood tutorial. Was stuck how to use java asa Back end for Flex 3. thanks.
I have FDS on my tomcat, will it work if i use the code given by you?
And also, how is navigation possible in Flex 3. For eg. after a user logs in, how do u redirect him to his home page???
Thank you
I've got a problem when a click over Say Hello! My web browser gets Frezee..."Transfering data from localhost" and there's not Alert on Screen saying "Hello Bruce". The first example worked good. This one no. Do you know something about this?
Thanks in advance
Cheers
How do I do that.
I have create a action script whcih matchs to the person class and tried it but getting and exception saying cannot convert to flex.messaging.io.amf.ASObject required example.Person class
Can you pls help.
Thank oyu