Grails: A Java web application in minutes

Posted by: on Mar 27, 2007 | No Comments

No Fluff Just Stuff have put up a great video interview explaining Grails and why it rocks. It’s really worth watching, even if you know Grails. It helped me focus on the benefits of Grails for users other than myself :)

Also, another one of the Grails developers reminded us in an email last week that it’s easy for us to forget the power of Grails’ agility in terms of getting from 0-60 in record time. I often have my head buried in my client projects or Grails internals, and sometimes forget this because I’m so used to it and working on existing projects.

So maybe it’s worth a little repeat session. Here’s a working web-app in minutes, based on Java, using Java code/libs if you want them, with Groovy glue code (or all your code for those bitten by the dynamic languages bug like me):

  1. Download and install Grails 0.4.2
  2. Open up a console/shell window
  3. Change to a directory where you want to create a project.
  4. Run: grails create-project GrailsTest
  5. Run: cd GrailsTest
  6. Run: grails create-controller Hello
  7. Edit: GrailsTest/grails-app/controllers/HelloController.groovy to read:
    class HelloController {
      def index = { render "Hello world" }
  8. Run: grails run-app
  9. Browse to http://localhost:8080/GrailsTest/hello

Of course this is trivial as it isn’t a full tutorial. So what do you have here? A web application that:

  • has a project structure already created and a working app with local HTTP server and in memory database
  • will build directly to a WAR file trivially (Run: grails war)
  • will reload most code changes immediately without a restart (edit HelloController again, change the string to "Goodbye World" and refresh your browser window!)
  • has SiteMesh built in
  • has MVC using GSP (Groovy Server Pages, like JSP but less sucky) or JSP views
  • has Hibernate preconfigured aand abstracted beautifully, ready to go (see GORM)
  • has convention-driven access to controllers
  • has Spring configuring beans and injecting services (more in a future article)
  • has support for custom Taglibs in views, much easier to write than JSP taglibs.
  • has built in unit testing infrastructure (Run: grails test-app)
  • has built in Ajax support

…and there’s so much more! Compare the time taken to create this working application, literally a handful of minutes, to the last time you started a new web-app project in Java using any framework.

Spend one lunch hour trying Grails and you may well be running to your project manager in the afternoon foaming at the mouth.