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

Why Grails?

Web development is a tricky business. Even a simple web application has a number of context changes ready to trip up the unwary developer. HTTP requests must be parsed and converted into internal code representations. Once parsed, the data must be validated to make sure no invalid or dangerous information has been sent. The data extracted from these requests is then persisted to rows in database tables. To send a response back to the user, data must be retrieved from the database and converted into the domain model. It is then rendered from the domain model into HTML format and sent back over HTTP. With every user action going through all these different conversions, we can see how web development can become expensive, and this is just the server side. We haven't even considered all the client-side coding that goes into web applications with the rich user experiences that are becoming the norm in the Web 2.0 era.

Over the years, there have been a number of frameworks created to alleviate the cost of building web applications. Web frameworks such as Struts, Spring MVC, Stripes, and JSF help in mapping the HTTP requests to code logic. Object to relational database mapping frameworks allow domain objects to be persisted directly to the database, Hibernate being the most notable. These frameworks have allowed larger and more complicated web applications to be implemented by reducing the workload of the application developer. Unfortunately, a side effect of multiple frameworks is an increased level of configuration that needs to be produced and maintained. The level of knowledge required to create framework configuration files is probably less than writing the code. However, it is notoriously difficult to debug and test the configuration.

Grails helps application developers provide value faster by:

  • Requiring less configuration
  • Faster setup
  • Shorter develop/test cycle
  • Consistent development environment
  • Domain-specific language for web development
  • Fewer dependencies

Less configuration

The first benefit Grails provides is a convention-based approach to remove the need for reams of configuration, while still leveraging the power of the mature underlying frameworks. In practice, this means that you don't spend a lot of time wiring your code together in XML configuration files, or muddy your code with endless annotations. Instead, if a class is created, according to the convention in the correct location, it will be wired into Spring as needed or will be treated as a Hibernate entity ready to be persisted in the database.

Faster setup

The convention based approach applies to your development environment as well as the code. As soon as you create a Grails project, you have a defined structure and a set of scripts already available to compile, test, run and package your project. Having all these scripts managed in a consistent and conventional manner greatly reduces the time required to get a project up and running.

Grails also comes configured with a bundled database and application server. So once you have Grails installed, and your project created, there is nothing else you need before you start development.

No longer do you need to spend time setting up a development environment for each project. Tweaking your Ant build scripts slightly for each new environment is a thing of the past, and so is configuring an application and database server for development.

Shorter develop/test cycle

Grails uses a bundled Jetty (http://www.mortbay.org/jetty/) installation for the application server, which is configured to execute against the working code base of your application. Grails is also built on top of Groovy — a dynamic language for the JVM that adds powerful new features to Java. Groovy compiles down to the Java bytecode, which allows it to integrate with any existing Java code. Chapter 4 introduces you to the Groovy language, if you have not used it before.

In development mode, Grails provides an auto-reloading feature (http://www.grails.org/Auto+Reloading), which allows you to make changes to your code and see the changes in your browser with the next refresh. There is no need to restart the application, or to re-deploy to an application server.

Consistent development environment

It is best practice in software development to try and ensure that all developers in a project are working in the same way and have a common environment on each machine. In reality, there are often conflicts between the configurations of different team member's development environments; team members may configure their application servers differently or use different database versions. You can waste valuable time debugging false problems in your software just because another team member has a configuration slightly different from yours.

Grails comes with a pre-defined application structure. This ensures that all developers will be working in the same way with the same environment configuration.

Domain-specific language for web development

Any experienced Java web developer will be familiar with the Servlet Specification, which provides a standard set of interfaces for working with HTTP. Grails builds on this specification and provides a DSL for developing web applications. The underlying specification is still available to developers, if they wish to use it, but in- depth knowledge is no longer required. By leveraging the flexibility of the Groovy language, Grails provides an intuitive and simple domain language specific to the web development, upon which you can build great web applications.

Fewer dependencies

The cost of getting up and running with Grails is remarkably low. You will need to download and install the following:

  • Java 1.5 or greater
  • Grails 1.1 or greater

Note that there is no need to download Groovy; it comes bundled with the Grails download.

Compare this to getting set up on a normal Java web project, where the typical download and install list would look something like this:

  • Java
  • DB server (for example, MySQL, HSQL DB)
  • Application server (for example, Tomcat)
  • Hibernate
  • Spring
  • Web framework (for example, Struts, Spring MVC)
  • View rendering framework (for example, Velocity, Freemarker)
  • Logging framework (for example, commons logging and Log4J)

You will eventually need to download and install an application server and a database server. Fortunately, this work can be put off until later down the line when you are thinking about deployment.