After a simple Grails application, let’s create a CRUD application. Such an application allows to perform basic database operations to read table rows from the database, create new rows, and edit and delete existing rows. This blog shows how such an application can be created using Grails, hosted on built-in Jetty servlet engine and use in-memory HSQLDB database for persistence.
A follow-up entry will show how this application can be deployed in production mode on GlassFish and using MySQL database.
- Create a Grails application
- After Grails download and configuration, create a new application “crud” as:
~/testbed/grails-1.0.2/samples >grails create-app crud Welcome to Grails 1.0.2 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Users/arungupta/testbed/grails-1.0.2Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples
Note: No plugin scripts found
Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateApp.groovy
Environment set to development
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/java
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/groovy
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/services
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/taglib
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/utils
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/layouts
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/i18n
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/unit
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/scripts
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/js
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/css
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/images
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/META-INF
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/lib
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/spring
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/hibernate
[propertyfile] Creating new property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
[copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud
[copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF
[copy] Copying 5 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF/tld
[copy] Copying 87 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
[copy] Copying 17 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
[propertyfile] Updating property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
Created Grails Application at /Users/arungupta/testbed/grails-1.0.2/samples/crud - In your project directory, create a domain class as:
~/testbed/grails-1.0.2/samples/crud >grails create-domain-class state Welcome to Grails 1.0.2 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Users/arungupta/testbed/grails-1.0.2Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
Note: No plugin scripts found
Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateDomainClass.groovy
Environment set to development
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
Created for State
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
Created Tests for StateThis creates “State.groovy” class in “grails-app/domain” directory and looks like:
class State { }
Add two fields to store state name and the corresponding abbreviation as shown below:
class State {
String name
String abbrev
} - Change “grails-app/conf/BootStrap.groovy” class to initialize the domain with sample data as shown below:
class BootStrap { def init = { servletContext
->
new State(name:”California”, abbrev:”CA”).save()
new State(name:”New York”, abbrev:”NY”).save()
new State(name:”Texas”, abbrev:”TX”).save()
new State(name:”Wisconsin”, abbrev:”WI”).save()
}
def destroy = {
}
} - Create a new controller as:
~/testbed/grails-1.0.2/samples/crud >grails create-controller state Welcome to Grails 1.0.2 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Users/arungupta/testbed/grails-1.0.2Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
Note: No plugin scripts found
Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateController.groovy
Environment set to development
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
Created Controller for State
[mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/state
[copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
Created ControllerTests for StateThis generates ”grails-app/controllers/StateController.groovy” with the following content:
class StateController { def index = { }
}Replace it with to define a scaffold:
class StateController { def scaffold = State
} - Start the application using built-in Jetty Servlet engine and in-memory HSQLDB database as shown below:
~/testbed/grails-1.0.2/samples/crud >grails run-app Welcome to Grails 1.0.2 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Users/arungupta/testbed/grails-1.0.2Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
Note: No plugin scripts found
Running script /Users/arungupta/testbed/grails-1.0.2/scripts/RunApp.groovy
Environment set to development
Running Grails application..
2008-04-18 17:26:29.962::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2008-04-18 17:26:29.075::INFO: jetty-6.1.4
2008-04-18 17:26:29.155::INFO: No Transaction manager found – if your webapp requires one, please configure one.
2008-04-18 17:26:30.886:/crud:INFO: Set web app root system property: ‘crud’ = [/Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/]
2008-04-18 17:26:30.886:/crud:INFO: Initializing Log4J from [file:/Users/arungupta/.grails/1.0.2/projects/crud/resources/log4j.properties]
2008-04-18 17:26:30.945:/crud:INFO: Initializing Spring root WebApplicationContext
[0] spring.GrailsWebApplicationContext Refreshing org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb: display name [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]; startup date [Fri Apr 18 17:26:32 PDT 2008]; parent: org.springframework.web.context.support.XmlWebApplicationContext@cddcc3
[1] spring.GrailsWebApplicationContext Bean factory for application context [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]: org.springframework.beans.factory.support.DefaultListableBeanFactory@11f136
2008-04-18 17:26:34.655:/crud:INFO: Initializing Spring FrameworkServlet ‘grails’
2008-04-18 17:26:35.716::INFO: Started SelectChannelConnector@0.0.0.0:8080
Server running. Browse to http://localhost:8080/crudThe application is now accessible at “http://localhost:8080/crud” and looks like:

- READ – Click on “StateController” and the following page is shown:

- CREATE – Click on “New State” and enter the values as shown below:

and click on “Create” to see the following page:

- UPDATE & DELETE – You can click on “Edit” or “Delete” to perform U or D of CRUD or click on “State List” to view the updated list as shown below:

Please leave suggestions on other TOTD that you’d like to see. A complete archive is available here.
Technorati: groovy grails glassfish jetty hsqldb scripting crud
Related posts:- TOTD #31: CRUD Application using Grails – Hosted on GlassFish and MySQL
- Getting Started with Grails on GlassFish
- TOTD #75: Getting Started with Grails using GlassFish v3 Embedded
- Grails on GlassFish v3 – Embedded or Stand-alone
- TOTD #80: Sinatra CRUD application using Haml templates with JRuby and GlassFish Gem
[Trackback] TOTD #30 explained how to create CRUD application using Grails and hosted using in-built Jetty servlet engine and in-memory HSQLDB database. Jetty and HSQLDB are good for development environment. However a robust environment such as GlassFish and MySQ…
Comment by Arun Gupta's Blog — April 23, 2008 @ 7:02 am
hi , you made a typo, it should be String in the class state :
class State {
string name
string abbrev
}
Comment by carol mcdonald — April 25, 2008 @ 9:39 am
Thanks Carol, fixed the typo!
Comment by Arun Gupta — April 25, 2008 @ 10:25 am
Hey Arun,
I have come across these simple steps in the grails official site itself. It would more helpful if you please explain how to create a full fledged application with grails because that is something I have not come across yet. I want to use grails but I am not quite sure how to introduce application features into it. And also please describe which groovy command to use when.
Cheers!
Comment by nitinpai — April 26, 2008 @ 12:40 pm
[Trackback] This Catalog Sample app demonstrates the usage of Groovy and Grails to implement pagination of data sets for a Store Catalog.
Comment by Carol McDonald's Blog — April 28, 2008 @ 7:37 pm
[Trackback] This Example application demonstrates the use of Groovy and Grails with the Java Persistence API running on Glassfish with MySQL
Comment by Carol McDonald's Blog — July 7, 2008 @ 7:56 pm