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:
[sourcecode language=”xml”]
<%@ page import="org.codehaus.groovy.grails.commons.ConfigurationHolder" contentType="text/xml;charset=UTF-8" %>
[/sourcecode]
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:
[sourcecode language=”xml”]
…
To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.


7 responses to “Flex on Grails, Take 2: Part 3”
[…] This post was mentioned on Twitter by Sebastien Arbogast, Burt Beckwith. Burt Beckwith said: RT @sarbogast: Flex on Grails, Take 2: Part 3 http://lnkd.in/-VfPw9 […]
[…] Read this article: Flex on Grails, Take 2: Part 3 | Epseelon […]
Many thanks to Sebastien and Burt for this great job! Came the blessed time when the plugin started to work well. =)
Is it possible to use this plugin to initialize MessageService in BlazeDS to send Messages to Flex-clients?
Sincerely, Manque.
Thank you Sebastien, you should write a book. Have been working with Flex/Blaze/Spring Integration/Spring Actionscript for two years now. Have been through the issues of Blaze not working with Groovy classes remotely and then Groovy not compiling in Roo (because of the AJ compiler). This should improve my efficiency a bunch.
Rick Holland
Thanks Sebastien. I’m trying to extend your module to allow service-crud-templating, where ideally you would only have to annotate the domain class and then automatically have the service and methods needed by Flash Builder’s Data Management exposed for the annotated domain class – by using an (possibly) extendable template service.
Any chance you would like to help me on that? I’ll have to warn you though: I’m a bloody beginner to grails plugin programming. 😉
Also, I just tried installing from git, and found a dependency conflict, in that acl requires spring security 1.0.1, whereas your plugin requires 1.1.2.
When importing as plugin project to spring tool suite, this results ambiguity, e.g.
[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.1.2grails-appconfDefaultSecurityConfig.groovy: -1: Invalid duplicate class definition of class DefaultSecurityConfig : The sources C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.1.2grails-appconfDefaultSecurityConfig.groovy and C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.0.1grails-appconfDefaultSecurityConfig.groovy are containing both a class of the name DefaultSecurityConfig.
[groovyc] @ line -1, column -1.C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.1.2grails-appservicesgrailspluginsspringsecuritySpringSecurityService.groovy: 29: Invalid duplicate class definition of class grails.plugins.springsecurity.SpringSecurityService : The sources C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.1.2grails-appservicesgrailspluginsspringsecuritySpringSecurityService.groovy and C:Usersjohn.cunliffe.grails1.2.3projectsgrails-blazedspluginsspring-security-core-1.0.1grails-appservicesgrailspluginsspringsecuritySpringSecurityService.groovy are containing both a class of the name grails.plugins.springsecurity.SpringSecurityService.
[groovyc] @ line 29, column 1.
[groovyc] class SpringSecurityService {
[groovyc] ^
[groovyc]
i know it’s probably a little too late but… I have a problem when configuring a custon exception Translator. All of a sudden all controllers are unreachable (give 404 error). Any hints on why this might be happening? Here’s my resources.groovy code:
(…)
// myExceptionTranslator(com.exceptions.MyExceptionTranslator) {}
defaultMessageTemplate(MessageTemplate)
xmlns flex: ‘http://www.springframework.org/schema/flex’
flex.’message-broker'{
flex.’exception-translator'(ref: “myExceptionTranslator”)
}
(…)