RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build Your First Grails Project: A Grails-Powered Blog : Page 3

Need to clear out the clutter of your Java web development? Say hello to coding by convention with Grails.

Validate the Domain
Once you have your basic domain model, you are going to need to add some validation rules to make sure the data is correct. You will define these rules for a Post:
  • Title is required.
  • Title must be less than 50 characters.
  • Teaser is optional.
  • Teaser must be less than 100 characters.
  • Content is required.
  • Published is required.

Domain constraints are specified in the class using the static constraints property. This property is a closure, which contains a number of Groovy builder nodes that represent each property in the Domain class. The Post class with your constraints specified will look like this:

class Post {

    static hasMany = [comments:Comment]

    String title
    String teaser
    String content
    Date lastUpdated
    Boolean published = false
    SortedSet comments

    static constraints = {
        title(nullable:false, blank:false, length:1..50)
        content(nullable:false, blank:false)

To verify these constraints are working, go back to your PostTests class and add a test case to make sure the title validation is correct:

void testTitleIsRequired() {
    Post post = new Post(teaser:"foo",

    post.title = "my title"

Grails provides a number of default constraints, including URL and email validation. You will need to use these constraints for your Commentator class:

class Commentator {

    static belongsTo = Comment

    String name
    String url
    String email
    Comment comment

    static constraints = {
        name(nullable:false, blank:false)
        url(nullable:true, blank:true, url:true)
        email(nullable:true, blank:true, email:true)        

Driving the Domain
The next step is to expose your domain objects to users through some controllers and views. This will allow you to manipulate your domain objects through a user interface. To create the controllers for your domain, go back to the command line and run the following commands:

grails create-controller Post

grails create-controller Comment

These will create two new classes in the grails-app/controllers directory called PostController and CommentController, respectively. If you take a look at the PostController, you will see there is not much to it—simply one closure called index:

class PostController {
  def index = { }

Grails uses a RESTful approach to mapping URL requests to controllers. You will employ this approach by adding a 'Hello World' output to the generated controller and accessing it once the application is running. The following simple controller example helps explain how to access a Grails controller from a URL:

class PostController {

    def index = {
        render("Hello world")

A closure represents an action on a controller and is directly accessible through a URL request. The index closure is the default action. You can override that by defining the defaultAction property on the controller. (See "Sidebar 1. The Render Method" for more on the render method beyond this simple example.)

You can now run the Grails application by going to the command line in your project directory and executing the following:

grails run-app

See Figure 1 for the output of calling the default action on the PostController.

Figure 1. Hello World Example: This page shows the output of calling the default action on the PostController.

You should now be able to access your index action via the URL http://localhost:8080/groovypublish/post/. That is, Grails starts up a Jetty server that listens on port 8080; groovypublish is the name of the project and the web application context; and post maps directly to the PostController. Because nothing else is specified, the default action will be executed.

To see how you can access named actions in a controller, update your PostController to contain a goodbye closure:

class PostController {

    def index = {
        render("Hello world")

    def goodbye = {
        render("Goodbye world")

It's that easy to add a new action to an existing controller.

Next, go to the URL http://localhost:8080/groovypublish/post/goodbye. You should see the message 'Goodbye world' displayed. Notice that the application was not restarted between these changes. Grails allows you to make changes to the code in your development environment on the fly.

Now that you have seen the basics of controllers and actions, you need to modify your controller to provide access to the data in your domain:

class PostController {

	def defaultAction = 'list'

def edit = {
    def post = Post.get(params.id)
    if(!post) {
        post = new Post()
    render(view:'edit', model:[post:post])

def list = {

The above code shows the edit and list actions on the PostController and makes the list action the default. The edit action loads an individual Post from the ID parameter (if the Post cannot be found, you create a new Post), puts the Post in the model, and renders the edit view. The list action loads all of the posts in order by the last updated date with the most recent first, puts it in the model, and then renders the list view.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date