Grails in Action

Glen Smith, Peter Ledbrook

Mentioned 7

Provides information on using the Groovy language with Grails to create applications, develop tag libraries, create domain models, and provide reusable services.

More on Amazon.com

Mentioned in questions and answers.

I'm a Java developer trying to learn Grails, and I'd like to get exposure to as many parts of the Grails framework as possible. Preferably doing so by solving small real-world problems using the "Grails way to do it" (DRY, convention-over-configuration, and so on).

Three example could be:

  • Learn about GORM by creating a few classes (say Movie, Actor, etc.) and relations/mappings between them (1:1, 1:N, N:N, etc.).
  • Learn about the layout support (sitemesh) by using it to generate headers and footers common to all GSP:s on the site.
  • Learn about the filter support by using it to make sure all accesses to a certain controller comes from authenticated users.

My question goes to all Grails developers out there - what would you include in a "Grails curriculum" and in what order?

All input appreciated!

Step 1 - Learn Groovy

If you already know Java, I highly recommend Programming Groovy. It's a lot more concise and up-to-date than the otherwise excellent Groovy in Action. Neither of these books cover the significant language changes in Groovy 1.6, so you should also read this page.

Step 2 - Learn Grails

The Definitive Guide to Grails is probably the standard Grails reference - make sure you get the second edition. Grails in Action is slightly more recent, but I haven't read it so can't comment further on it. I found TDGTG a little lighton GORM, so you might also consider checking out Grails Persistence with GORM and GSQL. It's a very short book, but worth it's weight in gold.

Step 3 - Dive In

Try and modify the sample app in the Grails book, or build your own from scratch. The Groovy console is a great way to experiment with snippets of Groovy code.

Our organization is planning to use Grails to realize a complex application with a REST interface. I have concerns on the scalability of the framework, given that:

  • domain classes will for sure make heavy use of polymorphism. I heard people have trouble with inheritance in Grails, for instance because of this problem on JIRA.

  • Trees of objects will be saved in the database. In GORM this would be something like:

    class Node
    {
      static def hasMany = [children: Node]
    }
    

    I thought we could benefit of cascading operation to manage dependencies, but I am not sure how it would work in this case.

  • the database is supposed to grow to store millions of objects and should work with Oracle, SQL Server and PostgreSQL. How realistic is it to count on Grails DB mapping to have this compatibility for free?

  • we plan to leverage custom domain constraints to check the consistency of domain objects. But I have the feeling that what you can do in a validator is limited (I am not sure if you can, for instance, load a set of other objects)

  • business logic implemented in services will be multi-threaded

Most examples in books and on the web show very simple application with CRUD operation on independent sets of objects.

I am afraid that I end up using a very small subset of the feature of Grails when the application grows, as scaffolding, web flows, ajax, or even the DB schema generated by domain classes may not correspond exactly to the needs of my application.

Has anybody experience to share on using Grails for such an application?

domain classes will for sure make heavy use of polymorphism. I heard people have trouble with inheritance in Grails, for instance because of this problem on JIRA.

Inheritance has improved in Grails 2.0. See the section on Abstract Inheritance. The issue mentioned in the JIRA bug reported can be worked around via declaring the property as transient.

Trees of objects will be saved in the database. In GORM this would be something like:

Self referential relationships are fine. For more info, see Grails in Action chapter 3.

the database is supposed to grow to store millions of objects and should work with Oracle, SQL Server and PostgreSQL. How realistic is it to count on Grails DB mapping to have this compatibility for free?

Grails is built on Hibernate, an enterprise quality ORM layer used in a variety of large J2EE applications. Depending on the types of queries you're running, you may need to use criteria and tweak things at that level, but Grails provides no inherent limitations that keep it from scaling.

we plan to leverage the custom domain constraint mechanism to check the consistency of domain objects. But I have the feeling what you can do in a validator is limited (I am not sure if you can, for instance, load a set of other objects)

You can do cross-field validation with this. You can also define your own custom validator classes.

business logic implemented in services will be multi-threaded

It is recommended not to store state in services, but you can use them in multi-threaded operations as there are different scopes available. Of course, the onus is then on you to control concurrent access.

Our organization is planning to use Grails to realize a complex application with a REST interface.

REST interfaces are fairly fun and easy with Grails. See Grails in Action Chapter 11 or Chapter 9/13 of Beginning Groovy and Grails from Novice to Professional.

Grails is built on tried and proven J2EE technologies like Spring and Hibernate. While the "out of the box" may only take you so far, the framework is configurable at various levels to meet your needs. So you're essentially asking if J2EE scales nicely. Many people seem to say yes.

Auto suggest/complete fields are used all over the web. Google has appeared to master it given that as soon as one types in a search query, suggestions are returned almost instantaneously.

I'm assuming the framework for achieving this involves a fast, in-memory data store on the web tier. We're building a Grails app based around retail products, so a user may search for Can which should suggest things like Canon, Cancun, etc, and wondering if a Java B-tree cached in memory would suffice for quick auto completes returned as JSON over AJAX. Outside of the jQuery AutoComplete field, do any frameworks and/or libraries exist to facilitate the development of this solution?

Autocomplete is a text matching, information retrieval problem. Implementing your own B-tree and writing your own logic to match words to other words is something you could do. But then you would have to implement Porter Stemming, a Vector Space Model, and a String-edit distance calculation.

...or you could use Lucene and its derivatives, which do a lot of this stuff already. If you really care about the data structures used to store this stuff, you could dive into its source. But I highly doubt writing your own and doing it all yourself would be more maintainable and efficient in the long run.

One of the more popular Grails ecosystem plugins for this is Searchable, which was mentioned in Ledbrook & Smith's Grails in Action. It uses Lucene under the covers, and make sit pretty easy to add full-text search to your domain classes. (For example, check out chapter 8 in GinA or the searchable docs).

I am a beginner in Grails 2.0 framework and I am trying to follow this tutorial http://grails.org/Simple+Avatar+Uploader . I implemented this code but I am getting an error in UserController.groovy at this line 'def user = User.current(session)' as ' No such property: User for class: grailtwitter.PersonController' I assume that this line takes the user's current session. I am looking for an explanation of how this works ?

This code is incomplete. The controller presupposes that you have a way of identifying the currently logged in user. Implicitly the line def user = User.current(session) assumes that you have defined a current() method on the user class itself that takes in a session and presumably uses some field that you've set in it to retrieve a User. That would be kind of dumb.

A common way to do this would be to build your own authentication mechanism. Note that this is a naive practice and you are far better off using Spring Security Core unless you want to leave your application open for gaping security holes. But, for practice, something like:

def login = {
    //if you're stupid enough to store your passwords in plain text and not sanitize user inputs, you deserve to be hacked
    def user = User.findByPassword(params.password) 
    if(user){
       session.user = user
    }
}

You could then replace the offending line in the tutorial (--def user = User.current(session)--) with

def user = session.user ?: new User(userid:"I'm a little teapot") 

Before you go much further, you probably want to walk through this free eBook on Grails before you get much further. Also highly recommend Grails in Action.

Hi I am new to Grails and I want it to study myself... so please suggest me good websites and blogs which has groovy examples and sample code snippets and cookbook examples I have found some.

http://mrhaki.blogspot.com/2009/12/groovy-goodness-getting-all_25.html http://www.groovyexamples.org

The official Grails reference document is excellent. There are also a number of good books available

we are just in the beginning of a new project. We want to use Grails on server side and GORM pages(for html pages rendering and JQuery support) and GWT(for rich gui backend interface). Later we planned to extend our application by devices for Android or Iphone. Therefore we planned to use a RESTful(with JSON) interface for all service access.

Now is my question is this approach a little bit overkill? Because we are now struggling to find the right structure for our REST interface. Of course we want to setup the interface so that I can be reused by all clients instead of providing e.g. GWT specific RPC service and the JQuery html interface is rendered by GORM only.

  • Is this approach a little bit overkill?
  • What is the easiest way to define a RESt service?(just look what for data I need, name the service and go for it?)
  • How would you suggest to put the rendering on HTML side? Only forward in the action to the appropriate view(gorm) and let JQuery only communicate via the REST service and rendering of JSON data instead of using gorm specific form logic?

Thanks for your help

Doesn't sound like overkill if you're planning to create mobile device clients. There are plenty of good resources on how to implement REST in Grails (I've been primarily following the book Grails in Action and this link, as well as this guide to RESTful services).

As for HTML rendering, in a simple web-only app I'd say just pass GORM objects into your normal views, that would probably be easiest. But GWT does have good support for services, plus if you use your services in the web front end, this will give you a complete set of usage scenarios for your REST services that you'll be able to use on the mobile device apps.

So I've done some reading on grails, mainly on object and the relationships, etc, so I get how to create domain classes and controllers and how they work and everything, but the connection I haven't made yet is how you can take that and design a working HTML interface and useable website with that. Can anyone suggest some further reading on that. I know I need to look more into how to use GSP's and stuff, but what else? Thanks in advance.

Links to websites rather than books would be preferable, but books are good too.

You can get started with basic scaffolding, the user guide discusses this in Chapter 16 at http://grails.org/doc/latest/ - you can either customize the dynamic scaffolding ('static scaffold = true') or run generate-all and edit the generated controller and views.

Dave Klein's book is a good intro and highly rated: http://www.amazon.com/dp/1934356468

Grails in Action - http://www.amazon.com/dp/1933988932 - and The Definitive Guide to Grails 2nd ed - http://www.amazon.com/dp/1590599950 - are also excellent and more comprehensive.

Before reading The Definitive Guide to Grails I would recommend Beginning Groovy and Grails http://www.amazon.com/dp/1430210451. It's only the basics of the language and framework, though I didn't read it to the end, it helped me a lot :) Both are excellent reads.

Btw.: Can anyone say something about the third one: Groovy and Grails Recipes (http://www.amazon.com/dp/143021600X)? Good? Bad? Waste of paper?

Check out the free e-book, Getting Started with Grails (http://www.infoq.com/minibooks/grails) it is not the best book on Grails I've come across but the price is right. I learned a lot from reading The Definitive Guide to Grails and would highly recommend that one; Burt Beckwith posted the amazon link above (http://www.amazon.com/dp/1590599950).