dcsimg

Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 2

It's the ultimate JVM Web Framework Smackdown! Three will enter the cage. Only one will exit the victor. Will it be JRuby on Rails? Groovy on Grails? Or Helma?

Having covered JRuby on Rails in part 1, we now turn our attention to Grails and Helma.

Grails: Start Nimble, Scale to Enterprise

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:

dataSource {
  pooled = true
  driverClassName = "com.mysql.jdbc.Driver"
  username = "root"
  password = ""
  url = "jdbc:mysql://localhost:3306/grailsPM?autoreconnect=true"
}
hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "update"
    }
  }
  test {
    dataSource {
      dbCreate = "update"
    }
  }
  production {
    dataSource {
      dbCreate = "update"
    }
  }
}

Any of the settings in dataSource can be overridden for specific environments. For instance, it is generally wiser to have a separate database for testing.

  ...
  test {
    dataSource {
      dbCreate = "create-drop"
      url = "jdbc:mysql://localhost:3306/pmTest?autoreconnect=true"
    }
  }
  ...

Again, this assumes the database pmTest 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.

class Song {
  static constraints = {
    trackNum()
    name(blank:false)
    playlistURL(url:true)
    durationInSeconds()
    numDownloads()
  }

  String name
  int trackNum
  String playlistURL
  int durationInSeconds
  int numDownloads
}

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:

class Album {
  static constraints = {
    publicID(blank:false, unique:true)
    title(blank:false)
    price()
    url(url:true)
    albumCoverURL(url:true)
    albumCoverName()
    blurb(maxSize:5000)
  }

  static hasMany = [song:Song,comment:Comment]
  static belongsTo = [artist:Artist,genre:Genre]

  String title
  String publicID
  String url
  String albumCoverURL
  String albumCoverName
  double price
  String blurb
}

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:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Welcome to LinmagMusic</title>
  <link href="${resource(dir:'css',file:'main.css')}" rel="stylesheet" type="text/css" />
  <link href="${resource(dir:'css',file:'pm.css')}" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="outer">
  <div id="header">
    <div id="logo">
      <img src="${resource(dir:'images',file:'logo.png')}"
           alt="PenguinMusic, your source for Free* Music" />
    </div>
    <img src="${resource(dir:'images',file:'linuxMagLogo.jpg')}" />
    <div id="toolbar">
        <g:link controller="mostPopular" action="index">Home</g:link>
      <g:if test="${session.user}">
      | <g:link controller="user" action="logout">Logout</g:link>
      </g:if>
      <g:else>
      | <g:link controller="user" action="login">Login</g:link>
      | <g:link controller="user" action="register">Register</g:link>
      </g:else>
      | <g:link controller="mostPopular" action="faq">FAQ</g:link>
    </div>
  </div>
  <hr />
  <g:if test="${flash.message}">
    <div class="message">${flash.message}</div>
  </g:if>

  <div id="body">
    <g:layoutBody />
  </div>
</div>

</body>
</html>

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:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="layout" content="main" />
    <title>Most popular songs</title>
  </head>
  <body>

    <div id="left_rail">
      <h2>Featured Artists</h2>
      <span class="featured_artists">
        <g:each in="${albumList}" var="${album}">
          <div class="featured-album">
            <em>${album.artist.name}</em><br />
            <strong>${album.title}</strong><br />
            <g:link controller="album" action="display" id="${album.id}">
              <img width="150" height="150"
                 src="${resource(dir:'albumArt',file:album.albumCoverName)}" />
            </g:link>
            <br /><br />
          </div>
        </g:each>
      </span>
    </div>
    <div class="body">
      <h1>Most Popular</h1>
      <div class="list">
        <table>
          <thead>
            <th>Song</th><th>Band</th><th>Album</th>
          </thead>
          <tbody>
            <g:each in="${songList}" var="${song}" status="i">
              <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                <td><a href="${song.playlistURL}">${song.name}</a></td>
                <td><em><a href="${song.album.url}">${song.album.artist.name}</a></em></td>
                <td><strong><g:link controller="album" action="display" 
                    id="${song.album.id}">${song.album.title}</g:link></strong></td>
              </tr>
            </g:each>
          </tbody>
        </table>
      </div>
      <div class="paginateButtons">
        <g:paginate total="${songInstanceTotal}" />
      </div>
    </div>
  </body>
</html>

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 ${ ... }.

Comments on "Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 2"

This is a topic that’s close to my heart… Many
thanks! Exaxtly where are your contact details though?

Also visit my web page cheap car insurance in va

Excellent blog here! Also your web site loads up fast!
Whaat web host are you using? Can I get your affiliatte link
to your host? I wish my website loaded up as quickly as yours lol

Also visit my page: cheap car insurance

Iall the time used to read piece of wrikting in news papers but now as I
am a user of internet thus from now I am using net ffor content,
thhanks to web.

Also visit my weblog … cheap car insurance florida

I visited multiple websites however the audio quality for audio songs existing at this site is genuinely
excellent.

The time to study or take a look at the content or web pages we have linked to beneath.

Here are some links to web-sites that we link to mainly because we consider they’re really worth visiting.

Hi! Do you use Twitter? I’d like to follow you if that would be ok.
I’m definitely enjoying your blog and look forward to new updates.

Also visit my web page :: Cheap Car Insurance

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.

Visit my blog: louboutin male shoes

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.

We like to honor a lot of other online web-sites around the web, even when they aren?t linked to us, by linking to them. Underneath are some webpages really worth checking out.

The facts talked about in the report are several of the top obtainable.

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.

Here are a number of the web pages we advise for our visitors.

One of our guests not too long ago suggested the following website.

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

My web-site :: cialis generic pharmacy

?????????????????????2.0MM-4.0MM??????????? 14 380Mpa???????????????????????????????,14,????????? ??? ???300g/m2,?????-5%?-???????????????????????????? ??????????????????????3???,13????????1,7.0MM-3.0MM???????????1380Mpa??????????????,??????????????PVC????????????????,??????? ??? ?-5%?-?????????????-5%?-?????????

Hello.This post was really remarkable, particularly since I was searching for thoughts on this matter last couple of days.

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.

Thank you

my site: mattress reviews

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.

Take a look at my webpage :: outlet christian louboutin

Very handful of websites that take place to become in depth below, from our point of view are undoubtedly properly worth checking out.

Hello to every , as I am in fact keen of reading this website’s post to be updated on a regular basis.
It consists of nice stuff.

Feel free to surf to my blog :: my latest blog post

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.

Also visit my site … mattress reviews

What’s up, yes thjis article is really nice and
I have learneed llot of things from it concerning blogging.
thanks.

Here is mmy site: dyson dc35

Nice answer back in return of this difficulty with
genuine arguments and describing all regarding that.

My website costco dyson

Nice replies in return of this query with real arguments and explaining the whole thing
on the topic of that.

my web-site :: dyson dc50

Every weekend i used to pay a quick visit this site, as i wish for enjoyment, for the reason that this this web page
conations really good funny material too.

Every weekend i used to pay a quick visit this site, as i wish for enjoyment, for the reason that this this
web page conations really good funny material too.

Every weekend i used to pay a quick visit this site, as i wish for enjoyment,
for the reason that this this web page conations really good funny material too.

Every weekend i used to pay a quick visit this site, as i wish for enjoyment, for the reason that this
this web page conations really good funny material too.

You can certainly see your skills within the work you write.
The world hopes for more passionate writers such as you who aren’t afraid to say how they believe.

Always follow your heart.

Feel free to visit my web blog mattress reviews

One of our guests recently recommended the following website.

It’s awesome to visit this site here and reading the views of all mates concerning this piece of writing, while I am
also keen of getting experience.

I think the admin of this website is genuinely working hard in favor of his web page,
for the reason that find out here now every information is
quality based stuff.

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.

Way cool! Soome extremely valid points! I apprehiate you penning this article plus the rest of tthe site is also really good.

my site – Adjustable Beds prices

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!

Here is myy webpage – mattress for adjustable bed

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!

My weblog Adjustable Beds and mattresses

Looking forward to reading more. Great blog post.Much thanks again. Great.

Consistently scrolling back and forth for viewing or reading the website’s contents can encourage be very frustrating coupons for food users michaels discount coupon I’m going to be reading another book
by a dorney park discount coupons In a little while kids
learns shared there ihop coupons
waste their time by hoping to see it with out working towards it ihop coupons they even get to achieve the lifestyle
that they have always longed-for universal studios discount coupon
You might have storage so use in which discount dance coupon codes

It’s perfect time to make some plans for the long run and it’s time to be happy.
I have learn this publish and if I may just
I desire to counsel you few interesting issues or advice.

Perhaps you can write next articles referring to this article.
I wish to learn more things about it!

my webpage android apk games cracked

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.

Auction know-how is something not everyone has, since online auctions are things we have not
yet been to discount coupons for car rentals but it does
enough to make it worth buy san diego zoo discount coupons Stone Steps
is found where a street called El Portal intersects Neptune best buy coupon code you will most
likely not please either the SF or the fantasy people discount tires coupon The homework is from workbook having a few problems or a photocopied published coupon discount tire Testinside
provide in depth knowledge each certification examination chicago coupons and discounts

Here is my homepage – discount coupon for zenni optical

Leave a Reply