Book Image

Grails 1.1 Web Application Development

By : Jon Dickinson
Book Image

Grails 1.1 Web Application Development

By: Jon Dickinson

Overview of this book

Web development is trickyóeven a simple web application has a number of context changes ready to trip up the unwary. Grails takes the everyday pain out of web application development, allowing us to focus on delivering real application logic and create seamless experiences that will address the needs of our users. This book will take the pain out of Grails by showing you exactly how to build a web application with a minimum of fuss. With this book, even if you are new to Grails, you will be up and running before you know it. You will be able to code faster and your code will be better. This clear and concise book is packed with examples and clear instructions to help you build your first Grails application and gives you the skills to speed up your application development by adding a different angle for learning about the topic. After a brief introduction to the dynamic JVM-based Groovy programming language, which teaches you enough about Groovy to understand the relationship between Grails and the Groovy scripting language, it shows how to use Grails and a number of key plug-ins to deliver valuable web applications. It also takes you through creating, developing, testing, and deploying an example team collaboration application in Grails. Using an incremental and iterative approach you will learn how to build a basic web application with secure authentication and different levels of authorization. You will learn how to handle file upload allowing users to share files. Some advanced features of object-oriented persistence will be introduced through adding tags for messages and files to giving users a robust categorization system. You will then build on the basic application to enhance the user experience through AJAX and the RichUI plug-in. You will take a further step into the world of Web 2.0 by adding an RSS feed and a REST service to the application. Once the entire application is up and running, you will learn how to create your own plug-in for tagging. Finally, you will learn how to deploy this application to a production environment.
Table of Contents (20 chapters)
Grails 1.1 Web Application Development
Credits
About the author
Acknowledgement
About the reviewers
Preface

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.

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

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

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:

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:

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