Inspired by Rails, other Web frameworks now incorporate many of its features. Grails is at the forefront of this group. In fact, at first blush, Grails mimics Rails so closely that it’s tricky to tell them apart. However, as you dig more deeply, differences begin to appear. Most importantly, Grails is built upon an enterprise-worthy Java stack. This includes Hibernate and Spring, two tools that many Java developers are already familiar with.

Whether Grails can truly scale to the enterprise level remains largely untested. What is easier to verify is that Grails adds many Java best practices to a Rails-like framework. While scriptlets are a possibility, they are generally eschewed in favor of tag libraries. Grails offers superior support for Unicode, and encourages developers to use message bundles. Both of the latter features make Grails a much better choice for internationalization.

Grails also seems more familiar to Java developers. While Groovy syntax can look unusual at times, Java developers can write much of their code using standard (pre-generics) Java syntax, and it works without a hassle. And although it’s not yet as well-established as Rails, Grails has a large and active community, a rich set of tools, and plugins for almost every major IDE.

Getting started

Installing Grails is a somewhat simpler process than installing JRoR. After you download Grails, set your $GRAILS_HOME environment variable and add $GRAILS_HOME/bin to your $PATH.

Let’s build PenguinMusic in Grails for comparison. The Grails implementation of PenguinMusic can be downloaded from here. Bootstrapping a Grails application is very similar to creating a new Rails application. The command grails create-app grailsPM creates a new project, where grailsPM is the project name.

As with Rails, the next step after code generation is to configure the database. Open grails-app/conf/DataSource.groovy
and edit the username, password, and URL to match your server settings.
Here is the sample version, which assumes the database grailsPM already exists:

dbCreate tells Grails what to do with the database. The options are create, create-drop, and update. create creates and modifies tables as needed, whereas create-drop deletes the tables on shutdown. Both versions delete all records. If you get test data from some outside process, update is probably the best choice. Also, for production, you may be better off removing this setting altogether and managing the database yourself.

Running grails run-app from the root of the project starts the application. Of course, there is nothing terribly interesting to see at this point, But now that the code is interacting with the database correctly, it is time to create some domain classes.

Domain Classes

One of the most notable differences between Grails and Rails is the design of the models, or domain classes in Grails terminology. With Rails, the structure of the models lives in the database, whether you create it with SQL or migrations. In contrast, with Grails, you never touch the database directly. Instead, you structure the data in Groovy code.

Grails comes with a noticeably richer set of constraints for the data models. It is also a decided plus to see the entire structure of a class located in one central place.

To create a new domain class for artists, run grails create-domain-class Artist. This creates a new empty domain class and the corresponding unit tests. Next, let’s add the fields for the domain class, including the corresponding types. It should look like this:

class Artist {
String name
String url
}

As with Rails migrations, more constraints can be added, though again the details live within the domain class. For example, here is another domain class, Song.

Grails’s constraints also serve the same role as Rails’s validation methods. The playlistURL field specifies that it should be a valid URL format. A similar constraint exists for email. Other constraints limit fields to a given set of values, or give more fine-grain control on the size of database tables. While Rails does have validation methods, they do not affect the structure of the database.

Domain classes can be wired together in the same manner as Rails’s models. The Album class is the most complicated. An Album belongs to an Artist, contains many Songs, and belongs to a given Genre. There also may be many Comments on an Album. The syntax for representing this is very similar to Rails, except that there is no hasAndBelongsToMany() method. Here is Album.groovy:

You can apply similar relationships to Artist.groovy and Song.groovy and the rest. When the application starts, the database is updated to match these classes.

Grails seems to trump Rails here. Grails does not require separate files to generate the database structure. Grails offers a significantly richer set of constraints, and only lacks the hasAndBelongsToMany() function for modeling the relationships between its domain classes.

But there is a dark side. With Grails, it is a little unclear how to update the database. Do you trust the update functionality not to mangle your production data? If not, updating production might force you to resort to SQL. At this point, you have returned to maintaining changes in two places.

It is unlikely that the Grails team will determine a good solution for this, as it seems to be an intrinsic issue with the domain classes specifying the available fields. In the end, whether you prefer the Rails or Grails approach may come down to a matter of taste; there is no clear winner.

Grails Views

Grails views are written in Groovy Server Pages (GSPs). Grails permits scriptlets, but it also offers a respectable tag library. As a result, Grails views look noticeably prettier than those in Rails. For example, here is the same sample layout used for Rails, but reformatted for Grails:

Yes, Grails offers layouts and partial rendering like Rails. Unfortunately, the layouts are a little more awkward to work with. The page-specific view code contains a good deal more extraneous tags. Here is the view for the “Most Popular Songs” page, which serves as the front page for the application:

The meta attribute is used to specify the layout. Otherwise, the header section is largely useless. It appears that the motivation here is to allow web designers to see a complete page, rather than forcing them to view little isolated bits, but the extra tags seem like at least a minor violation of the DRY philosophy. On the other hand, this version required no scriptlets. The g:link tags specified URLs. Outputting data uses just ${ ... }.

When it comes to conducting your business, your attireplays a supporting role.

Breezy summer dresses are a common favorite, as are flowing trouser pants.
This was one of the best releases in 2011,
known for its rich traditional look and a unique
combination of colors and material.

I simply want to mention I am just beginner to blogs and certainly savored this web site. More than likely I’m want to bookmark your website . You absolutely have excellent posts. Regards for revealing your webpage.

Organizational learning benefits a lot when they
end up missing your train/meeting/wedding. Mobile application development company whether they are
all kinds of games that are like Dragonvale then you are hill climb racing cheats
registered, you ll need a minimum of Android 4. But recently, only requiring you to
engage users is game that comes out, you can, in a simpler
and convenient manner. Angry puzzle game and a special version for the participant would have
been added in the way.

Organizational learning benefits a lot when they end up missing your
train/meeting/wedding. Mobile application development company whether they are all kinds of games that are like
Dragonvale then you are hill climb racing cheats registered, you ll
need a minimum of Android 4. But recently, only requiring you to engage users is game
that comes out, you can, in a simpler and convenient manner.
Angry puzzle game and a special version for the participant would
have been added in the way.

Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

Undeniably believe that which you stated. Your favourite reason appeared to be at the internet the easiest thing
to understand of. I say to you, I certainly get annoyed at
the same time as other folks think about concerns that they just do not recognize about.
You managed to hit the nail upon the highest
and also defined out the whole thing with no need side effect
, folks could take a signal. Will probably be again to get more.
Thank you

Definitely imagine that which you said. Your favorite reason appeared to be at the web the easiest thing to be mindful of.

I say to you, I certainly get irked at the same time as
folks consider issues that they just don’t recognize about.
You managed to hit the nail upon the top and also defined out the
entire thing without having side effect , people could
take a signal. Will probably be again to get more.

I have been exploring for a little bit for any high-quality articles or weblog posts on this sort of space . Exploring in Yahoo I at last stumbled upon this site. Studying this information So i¡¦m glad to express that I have an incredibly just right uncanny feeling I discovered just what I needed. I most no doubt will make certain to do not disregard this web site and give it a glance on a relentless basis.

That is why it is best to stick with the tried and true classics.
Near the end you see that Tonie controls this scene, but Konrad pushes back when she threatens him.
It is in fact a steadfast work in progress, yet the payoff is actually
spectacular.

Hey I know this is off topic but I was wondering if you knew of
any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time
and was hoping maybe you would have some experience with something
like this. Please let me know if you run into anything. I truly enjoy reading your blog and
I look forward to your new updates.

Are you a car show displays owner?
You have more than likely needed repairs in the past. You may experience some stress trying to locate a great vehicle repair company.
In this article, there are tips that can help you find a company you can trust.

Hello woulod you mind ltting me know which web host you’re utilizing?
I’ve loaded your bloig in 3 different webb browders andd I mjst say this blog loads a lott quicker then most.
Can you recommend a good hosting provvider at a fair price?
Thans a lot, I appreciate it!

Hi, I think your site might be having browser compatibility issues.
When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer,
itt has some overlapping. I just wanted to givve you a quick
headss up! Other thesn that, very good blog!

I simply want to say I am just all new to weblog and really liked you’re web page. Most likely I’m want to bookmark your website . You actually come with superb stories. Thanks a bunch for sharing your blog site.