Open your web browser and navigate to http://localhost:8080/helloworld/
You should see your first running Grails application.
Just Add Flex
Let us add our first Flex application. Open a text editor and type the following
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Label text="hello world"/>
</mx:Application>Save it as hello.mxml under your helloworld's web-app directory, so that the final path is similar to /DevDirectory/helloworld/web-app/hello.mxml
Point your browser to http://localhost:8080/helloworld/hello.mxml
Your computer should wait a little while, and you should be able to see a screen like this ( assuming you have FlashPlayer installed ):
Hot Compilation, oh my!
Let's change our hello.mxml a little:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Button label="click me" click="mylabel.visible=true"/>
<mx:Label id="mylabel" text="hello world" visible="false"/>
</mx:Application>All I've done here is to hide our label on startup and added an extra button. Navigate to http://localhost:8080/helloworld/hello.mxml again.
You will see that our file has once again changed. Click on the 'click me' button, and you will see the label appear.
What did we just do here? The Flex plugin comes bundled with a version of the Open Source Flex SDK. It is configured so that whenever there is a file with an extension .mxml is called, it automatically launches the compiler and creates a .swf file that is used by the Flash Player. Think of it as a java class file.
If you think about what we've done, it's quite remarkable. By downloading a small library from the web and setting paths, we were able to set up an entire Web Development framework configured with Spring, Hibernate and a Sitemesh web layer. Then we were able to add the Flex SDK, BlazeDS and configure hot compilation of Flex files. All with three lines of code!
Handling ErrorsLet's just break it and see what happens. Open the hello.mxml file again and add a non-sense tag at the end of the file
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Button label="click me" click="mylabel.visible=true"/>
<mx:Label id="mylabel" text="hello world" visible="false"/>
<mx:thisisnonsenseItellyou/>
</mx:Application>We go to http://localhost:8080/helloworld/hello.mxml and see that a helpful debugger screen has shown up.
That's a wrap! In this chapter, we learned how to install Grails and embed the Flex SDK into it via the Flex plugin. We also learned how to quickly add a flex view to our application that we can easily change or modify. I hope this chapter has showed you how easy it is to quickly get productive on Flex and Grails.