17
2011
Grails 1.4: The new Resources stuff
I’ve recently migrated one of my applications over to Grails 1.4M1 (mainly because I was busting to try out the new testing stuff that Peter was writing about). In the process, I had a chance to kick the tyres on the new resources infrastructure that Marc has baked into 1.4.
So what’s the verdict? The new Resources framework is jam-packed-full-of-awesome! This stuff has significantly reduced the amount of js/css dependency nightmares that I have been dealing in my view layer. Not only is there far less code to write, I can now migrate between versions of libraries without the drama of updating any of my ref links.
If, for no other reason, you need to jump on the new resource dependency freight train. The new resources infrastructure radically simplifies dealing with all your JavaScript and CSS files and their dependencies. If you’ve ever nicked any of those slick jQuery components you find on the web (or even if you’ve just tried to spark up jQueryUI), you know the pain that it is to include all those script tags all over your head section (and making sure you get them in the right order).
There’s only a few key concepts to learn:
- You can now define your resource components in a special configuration file (/grails-app/conf/MyAppResources.groovy)
- Resource definitions include their own dependencies (eg jQueryUI depends on jQuery)
- Consuming resources in your view layer is now a one-liner
- If you want to handle compressing, gzipping and caching of your resources later on, there are magic plugins.
First things first. Let’s say you want to include some jQuery component (such as this cute little growl library that I’ve been playing with for handling Grails messages). You’d normally copy the css into /web-app/css/growl and the js into /web-app/js/growl, then set about adding link tags into the pages that needed them. No more!
You now put all your resource definitions into /grails-app/conf/MyAppResources.groovy (or whatever you’d like to call it for your app, the convention needs to end in “Resources.groovy” though). Here’s a sample of what the file would look like:
modules = {
growl {
dependsOn 'jquery'
resource url:'/css/growl/growl.css'
resource url:'/js/growl/growl.js'
}
}
The jQuery plugin is installed by default in Grails 1.4, hence we can already “depend” on in here. The definition above tells the resources system, that any time we include the “growl” resource in a page, pull in jQuery, and also pull in the growl css and js resources in appropriate ways.
The most minimal definition would be:
<r:require modules="growl"/>
And you’re done! Giddy up! (actually, you will need to tweak your sitemesh layout too, more on this shortly)
The resources plugin will do the hard work of pull in all the files you need (even bundling the deps together for a faster download with less http requests). Have to be happy with that!
You will need to change your main sitemesh layout to put in r:layoutResources tags in both head and body. The resources plugin makes smart decisions about optimising placement of your code (for example, putting your JavaScript files at the end of the page for performance). So add one of these to both the head and body of your :
<r:layoutResources/>
There’s plenty more to explore in the new infrastructure, so head on over to the detailed documentation which is already in the Grails 1.4 docs or deep dive into the Resources plugin docs to get the skinny on neat debugging support.
1 Comment + Add Comment
Leave a comment
Glen Smith
Archives
- April 2012
- March 2012
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





[...] Glen Smith describes his experience with Marc Palmer’s resources framework in Grails 1.4 M1. [...]