July 23, 2008 3:28 PM
Struts has a bean:write tag which you use in your JSP to output the value of an object or an object's property. FxStruts is a free open source library that provides the same functionality as bean:write except that the output is in AMF or XML format. Simply point it to any plain java object and you get Flex friendly AMF or XML output with ActionErrors and transaction token support. This makes it easy to migrate an existing Struts app to a Flex UI: all you have to do is add new JSPs and change your struts-config.xml. There is an AMF/XML supporting version of the bean:message tag as well.

Web application development is tricky business. You start off with simple JSPs and after a while your web app is tag soup: a mix mash of JSP tags, HTML markup and server side code. You slowly realize there has to be a cleaner way of doing this. This is where you finally read about MVC, Struts, Spring, etc. and get blown away.

One of the advantages of following MVC is that it's easy to change the view layer. Once change your view layer to Flex, it is harder to get data from your model whilst using Struts. FxStruts was developed to make this part really easy.

To validate the fact about migration of existing Struts apps being easy using FxStruts, the Struts MailReader application has been modified to have a Flex UI without any changes to the Action classes. The only changes made were the addition of new JSPs and change of mappings in struts-config.xml.

Flex version of Struts Mailreader Application.

What are the pieces involved?

FxStruts is present both on the Java and Flex side.

There are two JARs that you have to add to your Struts web application. On the Flex side, there is no built-in component that makes a normal HTTP request and gets back AMF responses. The built-in HTTPService component has been extended to support that. This component (HTTPAMFService.swc) has to be linked to your Flex project.

Where do I get it?

FxStruts is hosted on code.google.com and has two pieces: the taglib piece licensed under the ASL 2.0 and the AMF / XML serialization part1 licensed under the LGPL 3.0. The flex component HTTPAMFService is licensed under the MPL 1.1.

The Flex version of the struts mailreader application (WAR) is available at code.google.com along with a walking tour highlighting the ideas concerned in developing a full fledged Flex application on top of Struts (similar to walking tour doc that ships with Struts Mailreader application). Also, documentation regarding installation and migration of an existing struts app to flex is available.

How do I use it?

Follow the installation instructions.

FxStruts offers you two new tags: fx:write and fx:message that you can use in your JSPs. Both tags take the same parameters as bean:message and bean:write2. These tags have a "type" attribute that can be set to AMF (default) or XML. XML is Flex friendly in the sense that once Flex decodes it to an actionscript object, it will match closely with the server side object.

Sample.jsp:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/lib/fxstruts.jar" prefix="fx" %>
<fx:write name="testObject" type="amf" errors="true" token="true" />

You can set the "error" attribute or the "token" attribute value to true if you want to nest error messages / transaction token (if any) within the result. This is to provide an alternative to the functionality of html:errors. An example usage of this is described in walking tour of Flex Struts Mailreader. Basically, you can have the error messages that would have come if you used the html:errors3 tag nested along with your AMF / XML result. Same goes for the transaction token.

The Struts action classes have functions saveToken(), validateToken() that lets you associate a unique ID to track a transaction along hits to multiple action classes. This is usually to enforce rules like, "page B has to be visited before page C". On the Flex side, you need to re-send the transaction token you received nested along with your result in the next HTTPService / HTTPAMFService call.

Fine Print

  • Struts 2 is not supported.
  • There has to be no stray newlines or characters in your JSP if you are using AMF serialization. AMF is a binary format and the taglib code accesses the output stream directly. So you'll get a "getOutputStream() has been already called" exception if you are not careful.
  • XML serialization has some limitations. AMF is the recommended type since it is lightweight, preserves type information and decodes really fast on the Flex end.
  • FxStruts has been tested using Struts 1.3.8 only.

More documentation!

A more detailed article walking through a simple hello world application is on the way. Stay tuned for more info.

Update: An Adobe Developer Network article on FxStruts has been posted.

Conclusion

With FxStruts, you can easily retrieve typed actionscript objects matching your server side Java objects from a Struts web application. This accelerates and smoothens the process of fitting a Flex frontend to your Struts web app.

CategoryFlex Comment(s)


[1] AMF serialization code has been flicked from BlazeDS.
[2] Difference from bean:write - There is no format / formatKey stuff.
[3] fx:write and fx:message also support all the attributes supported by html:errors though the html:errors name and property attributes are renamed as errorname and errorproperty.

Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: November 4, 2008 9:00 PM