Grails 1.1 Web Application Development
上QQ阅读APP看书,第一时间看更新

Installing Grails

Now that you have had the salesman's pitch for Grails, it's time to see if it can live up to the hype. So, let's get started.

Download Grails from http://www.grails.org and extract the downloaded files to your development folder. Create an environment variable called GRAILS_HOME and point it to the extract location.

Installing Grails

You will then need to add the %GRAILS_HOME%/bin to your path. It's that easy!

Installing Grails

While working on a Mac, you can modify the environment.plist file in the .MacOSX directory as shown in the following screenshot:

Installing Grails

Although Grails is built on top of Groovy, there is no need to install Groovy separately. Grails comes with the groovy-all-x.x.x.jar bundled and executes your Groovy code directly.

The first step is to create a new Grails application with the Grails script, ' create-app'. You will create a new application called 'teamwork'. Open up your command line, go to your development area and run:

>grails create-app teamwork

You should see something like the following output:

Welcome to Grails 1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: /tools/grails-1.1 ... Created Grails Application at <your_development_location>/teamwork

This will create a folder called teamwork and will set up your application structure within this folder. Verify that the application has been configured correctly. Go to the teamwork directory and check that you have a folder structure as shown in the following screenshot:

Installing Grails

The grails-app folder will contain the main source code for your application. By examining the layout of this folder, you can see the beginnings of the convention for the layout of your application. The Model View Controller (MVC) (http://java.sun.com/blueprints/patterns/MVC-detailed.html) pattern is enforced through this convention.

Here is the breakdown of the layout:

  • The domain directory contains the Model classes.
  • The views directory contains the view code.
  • The controller directory contains the controller files.
  • The conf directory contains any configuration code that we need to implement.
  • The i18n directory contains message bundles to support internationalization.
  • Helper services will reside in the classes that go into the services directory.
  • Tag libraries, which are refreshingly trivial to be implemented in Grails, reside in the taglib directory.

Once you have confirmed that the structure of your project directory is correct, go into the teamwork directory in your command line and run:

>grails run-app

Wait for the message, Server running. Browse to http://localhost:8080/teamwork, to appear in your command line. Then you can open a browser, and you will see the default Grails start page as shown in the following screenshot:

Installing Grails

This is an equivalent of your "Hello World" example, when using any other framework. The result is a Java application server running on port 8080 with your application deployed to the context teamwork. This is not bad going for a five-minute job.

Grails comes with Jetty and HSQLDB already configured, which is why we have been able to get an application up and running so quickly. Jetty is a Java application server that can be ran as an embedded component within any Java application. HSQLDB is a lightweight Java SQL database engine that can be run in-memory with minimal configuration.

Grails applications are packaged as a WAR file for deployment, and so, are not limited to running under Jetty. But developing in this environment provides several benefits including:

  • Fast deployment time
  • Pre-configured setup
  • Automatic reloading of code changes during development