Modified: 03/13/2008, Created: 10/29/2007
Get up and running a minimal web application using Grails.
The following concepts will be touched upon:
Also see the official Grails Quick Start
REM// set up Grails set DEV_TOOLS_DIR=C:\dev\tools set GRAILS_HOME=%DEV_TOOLS_DIR%\grails-1.0.2 set PATH=%GRAILS_HOME%\bin;%PATH%
grails help
grails create-app holidayminder
Here is a list of the files and directories created:

cd holidayminder
grails create-domain-class holiday
The above command created the files:
grails-app/domain/Holiday.groovy test/integration/HolidayTests.groovy
from
class Holiday {
}
to be
class Holiday {
static constraints = {
name(blank: true, nullable: true)
dateOfHoliday(blank: false, nullable: true)
dateCreated(nullable: true)
status(blank: false, inList: ['active', 'inactive'], nullable: true)
}
String name
Date dateOfHoliday
Date dateCreated
String status
String toString()
{
String strTemp = "Holiday: date: ${dateOfHoliday}; name: ${name}";
return strTemp;
}
}
if an Id is involved
if an Id is not involved
grails generate-all holiday
In the above command, holiday is the domain class name.
This command will create the following files
grails-app/controllers/HolidayController.groovy grails-app/views/holiday/create.gsp grails-app/views/holiday/edit.gsp grails-app/views/holiday/list.gsp grails-app/views/holiday/show.gsp
grails generate-controller holiday grails generate-views holiday
grails create-controller dbmaint.HolidayDb
This created the following files:
grails-app/controllers/dbmaint/HolidayDbController.groovy test/integration/dbmaint/HolidayDbControllerTests.groovy
and the directory
grails-app/views/holidayDb
package dbmaint
class HolidayDbController {
def index = { }
}
to be
package dbmaint
class HolidayDbController {
def scaffold = Holiday
}
grails dev run-app

<html>
<head>
<title><g:layoutTitle default="Grails" /></title>
<link rel="stylesheet" href="${createLinkTo(dir:'css',file:'main.css')}" />
<link rel="shortcut icon" href="${createLinkTo(dir:'images',file:'favicon.ico')}" type="image/x-icon" />
<g:layoutHead />
<g:javascript library="application" />
</head>
<body>
<div id="spinner" class="spinner" style="display:none;">
<img src="${createLinkTo(dir:'images',file:'spinner.gif')}" alt="Spinner" />
</div>
<div class="logo"><img src="${createLinkTo(dir:'images',file:'grails_logo.jpg')}" alt="Grails" /></div>
<g:layoutBody />
</body>
</html>
to be
<html>
<head>
<title><g:layoutTitle default="Grails" /></title>
<link rel="stylesheet" href="${createLinkTo(dir:'css',file:'main.css')}" />
<link rel="shortcut icon" href="${createLinkTo(dir:'images',file:'favicon.ico')}" type="image/x-icon" />
<g:layoutHead />
<g:javascript library="application" />
</head>
<body>
<div id="spinner" class="spinner" style="display:none;">
<img src="${createLinkTo(dir:'images',file:'spinner.gif')}" alt="Spinner" />
</div>
<div class="logo">
<!--
<img src="${createLinkTo(dir:'images',file:'grails_logo.jpg')}" alt="Grails" />
-->
Grails Web App
</div>
<g:layoutBody />
</body>
</html>

Edit the file grails-app/conf/BootStrap.groovy from
class BootStrap {
def init = { servletContext ->
}
def destroy = {
}
}
to be
import grails.util.GrailsUtil
class BootStrap
{
def init = { servletContext ->
println "**** BootStrap; GrailsUtil.environment: ${GrailsUtil.environment}"
switch (GrailsUtil.environment)
{
case "development":
println "**** BootStrap detected development"
configureForDevelopment()
break
case "test":
println "**** BootStrap detected test"
configureForTest()
break
case "production":
println "**** BootStrap detected production"
configureForProduction()
break
}
}
def destroy = {
}
//---------------------------------------------------------
/**
Tasks to do when Grails in running in dev environment.
*/
void configureForDevelopment()
{
println "BootStrap configureForDevelopment() called"
def dataItem = new Holiday(name: "New Year's Day 2007", dateOfHoliday: new Date(), dateCreated: new Date(), status:"active")
dataItem.save()
(dataItem = new Holiday(name: "New Year's Day 2007", dateOfHoliday: new Date(), dateCreated: new Date(), status:"active")).save()
(dataItem = new Holiday(name: "New Year's Day 2008", dateOfHoliday: new Date(), dateCreated: new Date(), status:"active")).save()
(dataItem = new Holiday(name: "New Year's Day 2009", dateOfHoliday: new Date(), dateCreated: new Date(), status:"active")).save()
}
//---------------------------------------------------------
void configureForTest()
{
println "BootStrap configureForTest() called"
}
//---------------------------------------------------------
void configureForProduction()
{
println "BootStrap configureForProduction() called"
}
}
Grails / Jetty runs on port 8080 by default. This is also the same default port number as Tomcat.
There are at least two ways to change the port number of the Grails (Jetty) web server.
Edit the file GRAILS_HOME/scripts/Init.groovy and change the phrase 8080 to another port number like 9090 .
When running the Grails web app, add a command line parameter of -Dserver.port=9090 before the run-app phrase.
Example:
grails -Dserver.port=9090 run-app
http://localhost:9090/holidayminder
To get a war file of your Gails app configured to use development environment, run the command
grails dev war
The war file will be located in the root of your project.
To get a war file of your Gails app configured to use the production environment, run the command
grails war
The war file will be located in the root of your project.