Mastering Grails 1.1 (Advanced): a 3-day class

Getting started with Grails is easy -- you can literally have your first application up and running in minutes. Thankfully, Grails grows in sophistication as you do. Just as Groovy allows you to do everything you can do in Java (and then some), Grails allows you to bring your existing experience with Spring and Java Enterprise Edition along for the ride and take it in new directions. Standards-based web technologies like CSS and RESTful web services are fully supported so you don't have to unlearn a thing. Come to this class to bring your web development prowess up a notch.

Objective

To take your basic Groovy and Grails skills to the next level. This is a hands-on, fast-paced, lab-intensive class where you'll learn by doing instead of reading about it in a book.

Goals

You will move beyond "stock" Grails features and peer beneath the covers a bit. The emphasis is on customizing Grails -- the default templates used for key components, the way that URLs get mapped, and even the build process using Gant (a Groovy implementation of Ant). You'll extend Grails by writing custom codecs to seamlessly encode and decode data, and explore type coercion so that you can write code like "Book.list() as ATOM". You'll also see various Domain-Specific Languages (DSLs) in Grails that make it trivial to do things like sending email, creating web flows, and configuring Spring.

You'll take advantage of the Grails event model to support things like user authentication and automatic timestamping. You'll leverage the HTTP lifecycle to support RESTful web services and Atom/RSS syndication feeds. You'll enable JMX support, manage file uploads, and much more.

Course Outline

Giving Grails a Facelift

In this section, you will dramatically change the look and feel of a Grails application. With a few CSS tweaks here and a couple of partial templates there, you'll end up with a web application that looks nothing like an out-of-the-box Grails application. Along the way, you'll also get a brief refresher on Grails features like scaffolding, auto-timestamping, modifying the default templates, creating a custom TagLib, and adjusting key configuration files like Bootstrap.groovy and URLMapper.groovy.

Authentication and Authorization

Knowing who your users are and what they are allowed to do are important if your website offers basic CRUD (Create, Retrieve, Update, and Delete) capabilities. We'll start with simple interceptors that enable before and after advice. We'll create a login form and demonstrate a simple strategy for encrypting the password in the database (as opposed to storing it in plaintext). Then we'll introduce JSecurity for a more advanced solution.

Codecs and Operator Overloading

In this section, you'll learn how operator overloading works in Groovy, and how you can customize it for your application. You'll see the basic codecs available to do things like URL-encode and -decode the name/value pairs in a querystring. From there, you'll create your own custom codec that metaprograms encodeAs() and decode() methods onto Strings. Finally, we'll discuss type coercion. By overloading the asType() method, you'll unlock the magic of the "as" keyword, enabling syntax like "Book.list() as XML".

Uploading Files

As users of Flickr and YouTube can tell you, not all web applications are read-only. In this section, you'll see how to upload a file from an HTML form. Spring's MultipartFile class makes it a breeze to check the original filename and size, as well as save the uploaded file to the web server's filesystem. Grails also supports persisting files in the database. We'll discuss the relative merits of each strategy.

RESTful Web Services

Grails applications can be a source of raw data -- in this case, XML -- as well as rendered HTML. You may be familiar with SOAP and service-oriented architectures (SOA). I'll show you how to create a resource-oriented architecture (ROA) based on the principles of Representational State Transfer (REST). HTTP verbs like GET, POST, PUT, and DELETE are all fully supported, and we'll take full advantage of them here.

Atom and Syndication

Ever notice those Atom icons that appear next to RSS links on blog pages? Don't be fooled into thinking that Atom is simply Yet Another Blogging Format. Atom is an Internet Engineering Task Force specification for data syndication. It is sophisticated enough to be the foundation of Google's GData - the RESTful set of APIs that replace their deprecated SOAP interface. In this section, we explore the hidden benefits of adding a temporal element to traditional Web services.

Indexing

If you're writing an intranet application that hides behind a firewall, Google will never find you. But your users will still expect a Google-like search experience. Never fear -- the Searchable plugin utilizes Compass and Lucene to offer just such a feature. We'll also look at Solr, an external service that wraps Lucene in a RESTful interface.

Mail

Sending email from a Grails application is easy, thanks to a simple mail DSL. Based on the Spring MailSender class, you can generate either plaintext or HTML messages. We'll also discuss using partial GSP templates to standardize the message content.

Web Flows

Sometimes you need to guide your users through a series of web pages in a specific order. (Think of an installation Wizard or the Amazon.com checkout process.) Grails offers a DSL for creating a web flow definition. Based on Spring Web Flow, you'll see how the familiar states, transitions, and events are configured in code instead of XML.

JMX and Spring Integration

In this section, you'll explore some enterprise-grade tools for monitoring and configuration. You'll learn how to instrument your Grails application with Java Management Extensions (JMX). You'll get an introduction to the Spring configuration DSL in Grails. You'll also see how the log4j settings are initially specified in Config.groovy, and how to adjust them dynamically with JMX

Customizing the Build Process with Gant

Everything in Grails, from build scripts to individual artifacts such as domain classes and controllers, throw events at key points during an application's life cycle. In this section, you'll learn how to set up listeners to catch these events and react to them with custom behavior.