Disclaimer:
I am not an expert in either Spring MVC or Flex and this article is not about fundamentals of Flex/BlazeDS. Its just how to get it working. Quickly.
Prologue:
I was playing around with Flex for the past couple of days. Being a Java developer, I was naturally inclined towards trying out BlazeDS so that I can use Flex as a front-end to my Java backend. The thing that I suffered from most was a huge information deluge. I was flooded by loads of blogs, articles and tutorials which made my life miserable. Finally, I saw a saviour in: Bare Bones BlazeDS Object Remoting, which explains how to create a BlazeDS and Java integration. Of course this does not include spring integration. Then I downloaded Spring BlazeDS, and went through the samples. Then I kind of combined these two and figured out how to make it work. The challenge here is that I have existing screens which are pure Spring MVC stuff. I am trying to embed flex in some of them, and have BlazeDS integration with my existing spring beans. Read on…
First things first: directory structure
My project directory looks like:
Details:
- src/main/java contains all my java files
- src/main/resources has all config files like hibernate.cfg.xml, property files, etc.
- src/main/webapp has the WEB-INF folder and the jsps
- src/main/webapp/flash has all the compiled .swf files to be embedded in the jsps
- src/main/flex has all the .mxml and action script files
Config files required for BlazeDS
You would need these three config files under the WEB-INF/flex directory:
services-config.xml
This is the most important file. This defines the various channels that would be used for client/server communication by the BlazeDS
<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service-include file-path="remoting-config.xml" /> <default-channels> <channel ref="my-amf" /> </default-channels> </services> <channels> <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint" /> <properties> <polling-enabled>false</polling-enabled> </properties> </channel-definition> </channels> <logging> <!-- You may also use flex.messaging.log.ServletLogTarget --> <target class="flex.messaging.log.ConsoleTarget" level="info"> <properties> <prefix>[BlazeDS] </prefix> <includeDate>false</includeDate> <includeTime>false</includeTime> <includeLevel>true</includeLevel> <includeCategory>false</includeCategory> </properties> <filters> <pattern>Endpoint.*</pattern> <pattern>Service.*</pattern> <pattern>Configuration</pattern> </filters> </target> </logging> </services-config>
remoting-config.xml
This defines the remoting adapter used by the flex client.
<?xml version="1.0" encoding="UTF-8"?> <service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> </adapters> <default-channels> <channel ref="my-amf"/> </default-channels> </service>
BlazeDS/Spring integration
To integrate Spring with BalzeDS, you have to touch upon these existing files:
web.xml
1. Adding the flex listener
Add the flex.messaging.HttpFlexSession listener.
2. URL mapping
Note that in the services-config.xml, the url of the default channel my-amf is http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf. You have to pass any url with this pattern to the Spring front end handler servlet, the org.springframework.web.servlet.DispatcherServlet. I assume that you already have a similar servlet defined for your Spring MVC. You should use the same servlet (so that you can re-use the same beans in flex) to map these urls. I am assuming the name of the existing DispatcherServlet is dispatcher.
This is how the modified web.xml looks like:
...
<!-- ******************* START flex **************************** -->
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring/messagebroker/*</url-pattern>
</servlet-mapping>
<!-- ******************* END flex **************************** -->
...dispatcher-servlet.xml
This xml already has the existing Spring MVC beans. This has to be modified to expose existing beans to the BlazeDS remoting as services. The xml header has to be modified as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd" default-lazy-init="true"> ...
Then, the body has to be modified by adding:
...
<!-- The default id of this bean is *_messageBroker*. It takes all config files by default.-->
<flex:message-broker/>
<bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter"/>
<!-- if this is not present, it gives a big exception -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- Maps all flex requests from blaze-ds to the flex message broker. You can re-use an existing bean instance -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/messagebroker/*=_messageBroker
</value>
</property>
<property name="order" value="0"/>
</bean>
...Finally, this is how you expose a bean as a service for remoting:
...
<bean id="accountGroupManager" class="com.swayam.exp.AccountGroupManager"/>
<!-- Expose spring bean to blazeds for remoting. This will be available as a remote object with an id *account* -->
<flex:remoting-destination ref="accountGroupManager" destination-id="account" />
...This is how the AccountGroupManager looks like:
public class AccountGroupManager { public AccountGroupManager() { System.out .println("*****************AccountGroupManager.AccountGroupManager()"); } public String save(String name, String desc) { // dummy code, returning static string return "40"; } }
Coding the mxml
I am using eclipse to code the NewAccountGroup.mxml. This is how it looks like:
<?xml version="1.0" encoding="UTF-8" ?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <!-- Associate client with BlazeDS destination via RemoteObject. --> <mx:RemoteObject id="accountGroupRemote" destination="account" /> <mx:Form> <mx:FormItem label="Group Name:"> <mx:TextInput id="groupName"/> </mx:FormItem> <mx:FormItem label="Description:"> <mx:TextInput id="description"/> </mx:FormItem> <mx:FormItem> <mx:Button label="Submit" click="accountGroupRemote.save(groupName.text, description.text);" /> </mx:FormItem> <mx:FormItem label="Server's Response"> <mx:Label text="{accountGroupRemote.save.lastResult}" /> </mx:FormItem> </mx:Form> </mx:Application>
Compiling mxmls to swf
The one thing you have to keep in mind is that, you have to compile the mxmls along with the services-config.xml. Note that you have something like url=”http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf” in there. While the server.name and server.port will be taken by default by the client, you have to specify the context.root during the compile time, as an argument. This is the context of your webapp. I am using the mxmlc that comes with the Flex SDK from the command prompt (you can add it to the PATH). First you cd to the src/main/flex directory. Here you go:
mxmlc -services ../webapp/WEB-INF/flex/services-config.xml -strict=true -debug=true -context-root=/ -show-actionscript-warnings=true -output ../webapp/flash/NewAccountGroup.swf NewAccountGroup.mxml
Note how you specify the services-config.xml.
So far so good. Now we will have to embed the generated flash into our jsp. This how it is done:
... <p> <object id="NewAccountGroup" align="middle" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="allowScriptAccess" value="sameDomain" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <embed src="/flash/NewAccountGroup.swf" width="1000" height="400" quality="high" bgcolor="#ffffff" name="NewAccountGroup" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object> </p> ...
Finally, just a word for Maven users, these are the arifacts you have to use:
...
<dependencies>
...
<!-- ******************* START flex **************************** -->
<!-- blaze ds -->
<dependency>
<groupId>com.adobe.blazeds</groupId>
<artifactId>blazeds-common</artifactId>
<version>${blazeds.version}</version>
</dependency>
<dependency>
<groupId>com.adobe.blazeds</groupId>
<artifactId>blazeds-core</artifactId>
<version>${blazeds.version}</version>
</dependency>
<dependency>
<groupId>com.adobe.blazeds</groupId>
<artifactId>blazeds-opt</artifactId>
<version>${blazeds.version}</version>
</dependency>
<dependency>
<groupId>com.adobe.blazeds</groupId>
<artifactId>blazeds-proxy</artifactId>
<version>${blazeds.version}</version>
</dependency>
<dependency>
<groupId>com.adobe.blazeds</groupId>
<artifactId>blazeds-remoting</artifactId>
<version>${blazeds.version}</version>
</dependency>
<!-- spring-flex -->
<dependency>
<groupId>org.springframework.flex</groupId>
<artifactId>spring-flex</artifactId>
<version>${spring.flex.version}</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>${xalan.version}</version>
</dependency>
<dependency>
<groupId>edu.oswego.util</groupId>
<artifactId>concurrent</artifactId>
<version>${concurrent.version}</version>
</dependency>
<!-- ******************* END flex **************************** -->
...
</dependencies>
...
<properties>
<blazeds.version>3.2.0.3978</blazeds.version>
<spring.flex.version>1.0.0.RELEASE</spring.flex.version>
<xalan.version>2.7.0</xalan.version>
<concurrent.version>1.3.3</concurrent.version>
</properties>
...For others, you can always expand the blazeds.war and put all the jars inside the WEB-INF/lib.
The only pain point for this approach is that every time you change your context root, you have to recompile all your flex against the services-config.xml along with a new context.root argument.
This is how the end screen looks like (in the browser):


