Monday, April 19, 2010

Building A Blog Using 7 Simple Steps In Grails

Building a regular web application using Grails should be the minimalistic activity that any Java developer may involved in. Just follow below 7 steps to develop your first Grails blog.

1. To create an application using Grails we should run a "grails create-app applicationName" command:

$> grails create-app

2. Change directory to the currently built directory of the application:

$> cd

3. At this point we create domain classes. Domain classes will consist the logic part of MVC model:

$> grails create-domain-class
$> grails create-domain-class
$> grails create-domain-class

4. To put some business policy into the model area we should modify domain classes

$> gedit grails-app/domain/com/datispars/blog/Content.groovy

You should modify Content class as below:


class Content {
String title
String body
Category category
Author author
static hasMany=[comments:Comment]
static constraints = {
title(blank:false, nullable: false, size:3..80)
body(blank:false, nullable:false,size:3..500)

$> gedit grails-app/domain/com/datispars/blog/Author.groovy


class Author {
String name
String email
String webpage
static constraints = {
name (blank:false, nullable:false, size:3..30,
email (email:true)
webpage (url:true)

String toString(){
return name;

$> gedit grails-app/domain/com/datispars/blog/Comment.groovy


class Comment {
static belongsTo=[content:Content]
Author author;
String comment
static constraints = {
comment (blank:false, nullable: false, size:5..200)
author (nullable: true) // Comments are allowed without a author

String toString(){
if (comment.size()>20){
return comment.substring(0,12);
} else
return comment;

5. To create the Controller part of MVC based on the domain classes you should run below commands:

$> grails generate-controller
$> grails generate-controller
$> grails generate-controller

6. Now you need to modify three controller that you have built to use Scaffold functionality of Grails:

$> gedit grails-app/controllers/com/datispars/

Scaffolding allows you to auto-generate a whole application for a given domain class. The simplest way to get started with scaffolding is to enable scaffolding via the "scaffold" property. So add the commented line (def scaffold = true) in the start part of the ContentController.groovy class:


class ContentController {

def scaffold = true // The only modification
// in the controller you should do...

static allowedMethods = [save: "POST", update: "POST",
delete: "POST"]

def index = {
redirect(action: "list", params: params)

Do just the same operatoion for AuthorController.groovy and CommentController.groovy (Same as 12 step) .

7. Run the application:

$> grails run-app

After running the server you should see below response in the terminal:

Server running. Browse to http://localhost:8080/

So, you browse http://localhost:8080/

1 comment:

  1. I'm delighted that I have observed this weblog. Finally anything not a junk, which we go through incredibly frequently. The website is lovingly serviced and kept up to date. So it need to be, thank you for sharing this with us.