Accessing Java objects with Flex remoting

This post talks about how we:

  1. Query the database through Flex by invoking a server side Java object (the ItemDao) by Flex remoting with Blaze DS.
  2. Displays the result, which is a java.util.List<Item> in a DataGrid and a ComboBox.

Now that we have successfully configured Spring with Flex, we would like to access server side Java objects, which we expose through Spring.

We will query Items from the database using Spring/Hibernate through the ItemDao.

This is how my Item.java looks like:

And this is how my ItemDao.java looks like:

As is obvious, I configure the ItemDao through Spring and then expose it as below:

The one thing to remember here is that, Flex handles remote access through call back mechanism. That is, the call to the Java object, is asynchronous. The call returns immediately. However, the result (or the error) is got asynchronously by registering suitable listeners.

The following code snippet illustrates this:

This is how it looks like:
flex_remote_object-1

The most important thing to note here is that the result that is got back in the call back function, should be cast with the proper data type in Flex. This is the link which has the details under the head Converting data from Java to ActionScript. From here we get to know the correct casting. In this case, we are casting the result into mx.collections.ArrayCollection since it is of java.util.List type.

Also note that though each element in the array is of the type Item, it can be accessed by its bean attribute name. That is how we can access the id, name and code in the DataGrid. Note that you cannot invoke the getter method like getId() on the object, it will not work.

So far, we have used a DataGrid which can extract data based on the attribute names. What if we use a ComboBox which needs a two dimensional array with data and label attributes? This is the code snippet of the modified getItemListResultHandler():

And this is the code to add the ComboBox:

The screen looks like this now:
flex_remote_object-2

Leave a Reply

Your email address will not be published. Required fields are marked *