Grails committer Jason Rudolph showed off the power of Grails at last night’s Northern Virginia Java Users Group (NovaJUG) using a technique guaranteed to impress. He started with a JDK, a Grails installation, and an empty directory. In a few minutes, he had a skeletal Java web application created and functioning in a web browser, ready for enhancement. Audience members literally oohed and aahed.
Grails is a rapid web application development framework for Groovy that allows Java developers to hold their heads higher when Rails advocates gloat about their Ruby framework. Grails provides both a development environment to make coding easier by generating code, and an MVC web framework with a servlet front controller, domain objects that easily tie into a database, and tag libraries for scripting Groovy Server Pages. “It’s absolutely everything you need, soup to nuts, to start building a web application,” he said.
Grails is in release 0.6, with a 1.0 release candidate currently in development. Since code is written in Groovy, Grails provides syntax enhancements and features unavailable directly in Java, but provides for full Java integration and reuse of existing Java libraries. Like Rails, Grails provides convention-over-configuration to minimize configuration files and tedious coding. It builds upon Spring and Hibernate.
During his presentation, Jason showed how to create domain classes, controller classes, and to customize GSP pages. He added constraints to domain values with simple declarations in the domain class, and showed how to change the default error messages that are displayed on the web page when constraint validation fails. It was refreshing to watch him make a simple change to a Groovy source file then see the result just by reloading the web page. No build step. No deployment step. In development mode, Grails watches for file changes and performs the build and deployment steps for you, he said. Grails writes regular messages to the log file when it performs this hidden work to remind you not to use this feature in production.
Jason touched upon some of the similarities and differences between Grails and Ruby on Rails. The philosophy of persistence is different. Grails considers your domain class to be the source of record. It talks to the database to create and update tables during development. Rails considers the database the source of record for domain entities, and creates domain classes appropriately. You can switch the database auto-create features off if you’re using an existing database, he said. The Grails development team is looking at using Middlegen in a future release to generate domain objects from an existing database schema, he said. Jason said a quality shared by Grails and Rails is they both work best when used on greenfield projects without an existing database or codebase.
Other features provided directly by Grails or through plugins:
- Custom URL mapping
- Alternate domain implementations, such as mapping domain objects to EJB3 entity beans
- Expose actions as web services
- Web page flow
- Many taglibs
- Authentication and authorization
- Integrating a search engine (e.g. Lucene).
Future releases are expected to support for:
- An object-relational mapping language
- Using JSP custom tag libraries inside GSP pages
- JPA integration
- Generating the domain classes from the database, as mentioned above.
In addition to working on Grails, Jason is a principal at consulting company Relevance LLC and the author of Getting Started With Grails (available for free download). He’ll be speaking next week in London at Grails eXchange, including a session on using EJB entity beans with Grails.