Monthly Archives: September 2009

Flex: mxml vs ActionScript

This might look rather silly, but still had to give in to temptation :). Since I had worked on Swings as well, had to see if I could code a create a popup window with ActionScript. Well, of course you can, but I am rather new to Flex and most of the tutorial, for some unknown reason, choose not to mention this topic. So I decided to give it a shot.

Below is a popup window coded in mxml. Its called ItemSelectDialog.mxml.

This is how you invoke it from another mxml:

And this is how its ActionScript counterpart looks like. Its called

You can use it just like its mxml counterpart, just make sure that you import the class.

This is how it looks like:

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 looks like:

And this is how my 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:

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: