 |
Hello Grails!
Posted by bleonard on January 30, 2008 at 06:37 PM | Comments (19)
NetBeans 6.1 M1 was just released, and with it comes rudimentary support for the Groovy language and the Grails framework. I say rudimentary because this is M1, but there's a lot of work in progress as you can see from this task list.
So, of course I'm anxious to compare Grails to Rails. What better way to do than by creating the same blog application I've been blogging about with Rails?
Setting Things Up
Unlike with Rails, the NetBeans Groovy plugin does not come bundled with Groovy and the Grails framework - these must be pre-installed on your system (don't worry, it's easy):
- Download and install Groovy
- Download and install Grails
- Download and install NetBeans 6.1 M1.
- Install the Groovy and Grails plugin (Tools > Plugins > Available Plugins)
- Open the NetBeans Options dialog and select the Groovy category. Set the Groovy and Grails home directories.
Creating the Grails Project
- Choose File > New Project to create a new Grails Application named GroovyWebLog. Your new Grails application will appear in the IDE:

Creating the Model
Or as Grails prefers to call them, Domain classes.
- Right-click the Domain classes node and select "Create new Domain Class":

- Name the class Post.
- When Post.groovy opens in the editor, add a single field, title:
class Post {
String title
}
Creating the Controller
- Right-click the Controllers node and select "Create new controller"
- Name the controller Blog.
- For now, we'll use dynamic scaffolding to create the application views at runtime. Replace the default contents of BlogController with the following:
class BlogController {
def scaffold = Post
}
Note, if our controller named matched the domain class name, we could then use def scaffolding = true. I kept the model and controller names different to match what we did with Rails.
Run the Application
- Right-click the GrailsWebLog project and select Grails > Run Application.
- If all goes well, NetBeans will start the Jetty server that comes bundled with Grails and launch your browser where you will see the Grails welcome page:

- Click the BlogController link:

- Create a New Post:

It's not immediately obvious, but the Id is a link which you can use to view the post details. From the detail page you can then edit and/or delete the post:

Adding Another Field
Our blog needs a body field.
- Edit Post.groovy and add a body field:
class Post {
String title
String body
}
- Return to the browser and depending on what you do, you may get an error. By default Grails is configured to drop and create the database every time a change is made, so if you tried to edit the existing record you were viewing, you got a NullPointerException. However, if you navigate back to the list, you will see that the body field has been detected and you can add a new record again:

A Friendlier Database Configuration
Surprisingly, we haven't had to talk about databases yet as the Grails framework magically took care of this for us. The Grails framework includes HSQLDB and all of the necessary configuration was setup for us in DataSource.groovy, which you'll find under the Configuration node:

You can also see from this configuration file that Grails supports the same three environments as Rails: development, test and production. It's also easy to switch to another database, just set the driver class name, jdbc url and copy the JDBC driver to the project's lib directory.
Do the following:
- Change the dbCreate property from "create-drop" to "update".
- Right-click the GrailsWebLog project and choose Grails > Stop Application.
- Richt-click the project again and choose Grails > Run Application.
- Now experiment with adding and/or deleting fields - you'll notice that the behavior is much more "Railsesque".
Validating Input
Like Rails, validation in Grails is very straightforward.
- Open Post.groovy and add the following constraints:
class Post {
String title
String body
static constraints = {
title(blank:false)
body(blank:false)
}
}
- Attempt to add a new Post without entering any data:

Customizing the View
Okay, enough with this dynamic scaffolding. Let's generate our controller and view code. Unfortunately, for this step I haven't found the menu option in NetBeans yet, so we'll resort to the command line for now.
- Open your command prompt and navigate to the GrailsWebLog project directory. If you've forgotten where this is, you can find it in the project's Properties dialog.
- Run the command grails generate-all Post:

- Now this created a new controller named PostController. I don't see a way to specify a controller name when using generate-all (or generate-controller). That's fine - I'll leave BlogController to do the dynamic scaffolding and use PostController for my customizations.
- Also created were a set of GSP (Groovy Server Pages) for managing the view for the PostController:

- Open list.gsp and delete everything from from the <h1>Post List<h1> to the </div> following the pagination buttons. In its place put the following:
<h1>The Groovy Blog</h1>
<g:each in="${postList}" var="post">
<h2>${post.title}</h2>
<p>${post.body}</p>
<small><g:link action="show" id="${post.id}">permalink</g:link></small>
<hr>
</g:each>
- Return to the browser and refresh the list page:

- To display the blog with the most recent entry first, add .reverse() to the end of postList in list.gsp:
<g:each in="${postList.reverse()}" var="post">

The Completed Application
GrailsWebLog.zip
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Why is installing Groovy needed? Doesn't Grails include Groovy already?
Posted by: hsalameh on January 31, 2008 at 09:54 AM
-
In other words, is step 1 under "Setting Things Up" really needed?
What i did was download only Grails(which should already include Groovy), and then for what you have done under step 5, i set both home directories to the location of the grails installation, but all what i got was a lot of exceptions when trying to use the grails actions in NB. Is what I did incorrect?
Posted by: hsalameh on January 31, 2008 at 10:10 AM
-
Yes, Grails does include Groovy already, but like you, I couldn't get it to work w/out also installing Groovy separately. NetBeans may be dependant on something additional that ships with Groovy. A better place for this question would probably be the scripting e-mail alias.
Posted by: bleonard on January 31, 2008 at 11:59 AM
-
Hi,
I have a couple problems. #1 Runnin ghte application stops at "Running Grails application..". No server is available at port 8080 and the web browser does not launch. Manually executing the command from the windows console works.
Once I have the server running with the scaffolding. Should edit and delete work? Both result in a Error 404 for me. Is that default behaviour for editing not to work right away with a scaffold or is my grails broken?
Thanks,
Aaron Broad
Posted by: aaron_broad on January 31, 2008 at 11:53 PM
-
These steps don't work for me. Installing went fine, but when creating the domain class, I receive the following error message: java.lang.IllegalArgumentException: Called DataObject.find on null. I cannot seem to find a way to get around this. I already started over with a fresh userdir, but keep getting this error.
Posted by: bvaessen on February 01, 2008 at 12:28 AM
-
Hi Aaron. For problem #1, typically that occurred for me when there was some kind of build error, but if the app runs okay from the command line, I don't know what's going on. For problem #2, interesting find - edit and delete do not work for me either until I generate the view code. This appears to be because the Domain Class and Controller are named differently. I just filed this as an issue with Grails. /Brian
Posted by: bleonard on February 01, 2008 at 08:28 AM
-
Ben, are you able to successfully run the command "grails create-domain-class Post" from the command line (make sure you're in the project directory). -Brian
Posted by: bleonard on February 01, 2008 at 08:32 AM
-
Doesn't work for me :-( I install M1, and it works fine... Then install the Groovy plugin, and it won't start up again
Netbeans throws the followingjava.lang.NoSuchMethodError: org.netbeans.Stamps.getModulesJARs()Lorg/netbeans/Stamps;
Posted by: timyates on February 01, 2008 at 08:48 AM
-
(on OS X 10.4 btw)
Posted by: timyates on February 01, 2008 at 08:48 AM
-
Tim, by chance have you used development builds of NetBeans in the past? If so, it would be a good idea to delete your user directory (~/.netbeans/dev). After doing so you'll need to install the Groovy and Grails plugin again. I'm also on OS X 10.4 so it should work for you. -Brian
Posted by: bleonard on February 01, 2008 at 10:22 AM
-
Yeah, I've tried removing my entire ~/.netbeans folder, but still no joy... I'll try it again, it's probably me being an idiot...
Posted by: timyates on February 01, 2008 at 10:47 AM
-
Tim, I had the same problem with 6.1M1 for WinXP. Try the latest build (I installed todays) and it should work just fine.
Posted by: jeremygerrits on February 01, 2008 at 11:06 AM
-
Got it :-) I needed the "All" install, not just the Java SE one... Something must be missing from Java SE that the Groovy stuff requires :-) As you were people... ;-)
Posted by: timyates on February 01, 2008 at 11:12 AM
-
Tim, good catch. The issue's being tracked here in case you want to watch it. -Brian
Posted by: bleonard on February 01, 2008 at 12:52 PM
-
Brian, Sorry for my late response. I just tried to create the Domain Class from the command line, so this works.
Posted by: bvaessen on February 06, 2008 at 12:04 AM
-
Brian, I have done some extra tries and found out that when I create my project on my D:-drive (in Windows) the error occurs. I tried to create it on my C:-drive as well and then the error does not occur. Seems like a bug to me...
Posted by: bvaessen on February 06, 2008 at 02:20 AM
-
Hi *,
the problem with having grails and the project on different drives is fixed, see:
http://www.netbeans.org/issues/show_bug.cgi?id=127531
Please file all issues within issuezilla (category groovy) and give feedback what's missing.
Posted by: schmidtm on February 25, 2008 at 03:56 AM
-
Wheres the plugin for 6.1 Beta?
Posted by: shemnon on March 12, 2008 at 08:23 AM
-
Yes, I was wondering that as well. I installed the development updated center into the Beta (Tools > Plugins > Settings > Add). Here's the URL: http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/updates.xml.gz . It appears to be working fine - I can't vouch for any of the other plugins on that site :-). /Brian
Posted by: bleonard on March 13, 2008 at 08:48 AM
|