Pages

Wednesday, 21 March 2012

Sample Remoting Application Using BlazeDS in flex with Java Object

 

After the configuration done with flex and blazeds Now you are ready to create an application in BlazeDS.BlazeDS communicates with flex using remoting-config.xml in which you will create the destinations. These destinations will refer to the java Objectswhich are available in java source path.

At first create a java Class HelloWorld.java. This class has a public function and will return a string message to the flex.


HelloWorld.java:

 
package test;
public class HelloWorld{
           
      public String getMessage()
      {
     
            String Message=new String("hi from java");
            return Message;
                 
      }
}

Use the <mx:RemoteObject> tag to represent an HTTPService object in an MXML file. This tag gives you access to the methods of Java objects using Action Message Format (AMF) encoding

In Flex side you have to create a Mxml file which will have the remote object tag.The following is the mxml file invokes the remote java object.

Helloworld.mxml:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  horizontalAlign="center" >
      <mx:Script>
            <![CDATA[
                  import mx.rpc.events.ResultEvent;
                  import mx.controls.Alert;
                 
                  public function retrive():void
                  {
                  helloworldrm.getMessage();   
                  }
                  public function showMessage(event:ResultEvent):void{
                  var message:String;
                  message=event.result as String;
                  Alert.show(message);
                       
                  }
            ]]>
      </mx:Script>
     
      <mx:RemoteObject id="helloworldrm" destination="helloWorld" result="showMessage(event);" fault="Alert.show(event.fault.faultString);"/>
      <mx:Button id="blazeds" click="retrive();" label="Get Message form Java"/>
     
</mx:Application>


AYou can add the destination in the remoting-config.xml as following and also you can create as many number of destinations as you want.

Remote-config.xml


<destination id="helloWorld">
<properties>
<source>test.HelloWorld</source>
</properties>
<adapter ref="java-object"></adapter>
</destination>
     










No comments:

Post a Comment