At the end of the second article in this series, we ended up with a working application but it was not really ready for the real world because it had one major flaw: the URL of the AMF endpoint was hardcoded in the client in such a way that it was impossible to change after compilation and very hard to handle several environments (dev, test, prod). The solution to that problem is to integrate dependency injection into the mix.
Now there are a lot of such frameworks for Flex/ActionScript applications, including Parsley, Swiz, Cairngorm, etc. But I’ve never been a big fan of those big MVC frameworks that impose their own interpretation of the MVC pattern and completely limit the initial capabilities of Flex itself. For me, the Flex framework itself is clean enough that you don’t need all that overhead and it’s better to use a non-intrusive framework like Spring ActionScript. So that’s what we are going to do.
First off, go download the latest release of Spring ActionScript with its dependencies. Unzip this distribution and copy all four as3commons libraries under lib/ into the “libs” directory of your Flex project. Do the same with spring-actionscript-core-1.1.swc under dist/. You should now have 5 SWC’s in your libs folder:
Now, like its Java big brother, Spring ActionScript configures itself thanks to an XML application context file. And we are going to configure our AMF channel and our channel set in that file. We don’t want to embed that file into our Flex application because if we do so, we won’t be able to change it dynamically. And we want its content to be synchronized with the current environment. That’s why we will use a little trick here.
First, in your Grails application, create a new controller called org.epseelon.todolist.controllers.ApplicationcontextController. To do that, you can use the following command: “grails create-controller org.epseelon.todolist.controllers.Applicationcontext”. You can also use IntelliJ’s shortcuts: right-click the grails-app/controllers directory and choose “New Grails Controller”:
And in the dialog that appears, enter “org.epseelon.todolist.controllers.Applicationcontext”:
Now you can leave the ApplicationcontextController class as it is. But if you created your controller with IntelliJ IDEA, you should have a new index.gsp file under grails-app/views/applicationcontext. We are going to modify that file and use the fact that Grails can render XML templates and HTML templates in the same way:
<%@ page import="org.codehaus.groovy.grails.commons.ConfigurationHolder" contentType="text/xml;charset=UTF-8" %>
The first thing to notice here is the content type of this GSP, which is text/xml and not text/html. We also import ConfigurationHolder which will allow us to user the environment-specific server URL as defined in grails-app/conf/Config.groovy without having to load it from the controller. Our AMF endpoint URL is now configured in the second argument of the constructor for the first bean. What this application context does is that it configures an AMF channel and a channel set that we will be able to inject into our Flex application. But you can already test this controller by starting your Grails application and going to http://localhost:8080/todolist/applicationcontext.xml. You might wonder where this extension comes from: Grails uses the extension as an indication of the context type that is expected as a response, which makes it possible get different response formats from the same controller action just by changing the extension. Here we are just using the fact that by default, an empty action renders a GSP view with the same name as the action (index) and the content type declared in the GSP corresponds to the one requested.
The next step is to load that application context into our Flex application. To do that, let’s add a couple of event handlers to our s:Application element:
To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.