Daniel Harfleet: Integrating Java and Flex with a simple RPC example

« Java Development in Flex Builder | Main | Calling Java remote objects and handling results »

July 13, 2006

Integrating Java and Flex with a simple RPC example

In the last coffee break guide you will recall that we set up Eclipse to allow coding of java within a flex project, you will also remember that we created a simple java class with an 'echo' method on it. In this guide, we will get the flex code (i.e. the flash client) to call the java server code.

First of all you need to do a little bit of config, under WEB-INF/flex, edit remoting-config.xml, adding in:


<destination id="echoService">
<properties>
<source>fdsweb.EchoService</source>
<scope>application</scope>
</properties>
</destination>

Inside the service tag. This allows the flex client to access the remote java class, you can read more about this in the docs, the above will work for you now.

Double check your class file has compiled and is under WEB-INF/classes.

Now you need to write a simple flex client to the java class, to save you time, I have done this, so create an empty Flex Application mxml file by doing: New>MXML Application and call it EchoView, leave the layout to its default.

You should have an mxml file now with an empty application tag inside it. Replace all of the contents of the file with this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
	
   <!-- VIEW CODE -->
	
   <mx:Label text="Echo Service Simple RPC Example" fontSize="20" />	 
	    
   <mx:Spacer height="20" />
    
   <mx:TextInput id="messageInput" />
    
   <mx:HBox>
    			
      <mx:Button label="send" click="handleSend(messageInput.text);" />
      <mx:Button label="clear" click="handleClear();" visible="false" id="clear" />
		    	
   </mx:HBox>

<mx:Label id="resultLabel" />


<!-- REPRESENTS THE REMOTE OBJECT -->
<mx:RemoteObject
id="echoRO"
destination="echoService"
fault="handleFault(event)"
result="handleResult(event)" />


<!-- EVENT HANDLING CODE -->
<mx:Script><![CDATA[

import mx.controls.Alert;

private function handleSend( message : String ) : void
{
resultLabel.text = "";
echoRO.echo(message);
clear.visible = true;
}

private function handleClear() : void
{
resultLabel.text = "";
messageInput.text = "";
clear.visible = false;
}

private function handleResult( event : Object ) : void
{
//resultLabel.text = "" + event.result ;
resultLabel.text = event.result as String ;
}

private function handleFault( event : Object ) : void
{
Alert.show( event.fault.faultCode + "\n" + event.fault.faultString, "fault" );
}

]]> </mx:Script>

</mx:Application>


Right click on EchoView.mxml and select run as > Flex Application

Your browser should then show our flex app with a text input and a button, give it a try by typing something in the text field and hitting send

In the next guide, I will give you a quick overview of the flex code which calls the java and handles the results.

Posted by dharfleet at July 13, 2006 12:20 PM

Comments