t Grails Web Lab 1
test page for Grails projects
Key Points
- Grails scaffolding and GORM is IBM i DFU on steroids - any database, any Java runtime environment
- Where it fits, Grails is the fastest way to build Web apps or microservices using the right templates
- Grails generated apps provide a good starting point to understand how Web and REST microservices are built
- Grails generates Spring Boot applications and REST services
- Grails can build traditional MVC web apps or REST microservices using templates and generators
- Grails can generate simple front-ends for REST microservices - Angular, React, Web
- Grails profiles define which options an application or service will use
- Grails uses Groovy - a Java superset and a dynamic JVM language
- Grails v4x will use Groovy v3x when available in 2020
- Groovy v3x will support latest Java versions ( Lamba expressions, modules etc )
- I should look at running Grails REST service in Docker container similar to a Spring Boot REST service
- Tracking Grails releases in Github ( milestones etc ) https://github.com/grails/grails-core/releases
References
Test References
Find grails scripts, files and apps
from ~/
Groovy, Grails scripts
find ./ -iregex ".*setGr.*" 2>/dev/null -exec ls -l {} \;
Groovy work folders & files
find ./ -iregex ".*groovy" 2>/dev/null -exec ls -l {} \; | more
find ./ -ctime -150d -iregex ".*groovy" 2>/dev/null -exec ls -l {} \; | more
gdrv/_work/
use_case_template1.groovy
find ./ -ctime -120d -iregex ".*groovy" 2>/dev/null -exec ls -l {} \; | more
-rw-r--r--@ 1 jimmason staff 372220 Jul 22 09:09 .//learn/blearn-pc/gtest6.groovy
-rw-r--r-- 1 jimmason staff 1807 Dec 9 19:06 .//sfw/gdsl_base.groovy
-rw-r--r-- 1 jimmason staff 1944 Dec 13 18:52 .//sfw/use_case_template1.groovy
Grails apps list
find ./ -iregex ".*spring\/resources.groovy.*" 2>/dev/null -exec ls -l {} \; > ./temp/_grails_apps_list1.txt
gdrv files
gtwf1
gdrv//_work/learn/blearn-pc/gtwf1/grails-app/views/layouts/main.gsp.gz
gdrv//_work/jim-acer/blearn/gtwf1/grails-app/init/gtwf1/BootStrap.groovy
MAC files
grails4
jim-macbook:_dev jimmason$ ls -l ~/_dev/grails4
total 32
ptp test scripts
jim-macbook:~ jimmason$ ls -l _dev/_gdev
total 576
-rw-r--r-- 1 jimmason staff 2295 Aug 24 2015 JdbcTester1.groovy
-rw-r--r-- 1 jimmason staff 3727 Aug 31 2015 RunJdbcTester1.groovy
drwxr-xr-x 16 jimmason staff 512 Feb 19 2015 bookstore
drwxr-xr-x 3 jimmason staff 96 Aug 28 2015 com
drwxr-xr-x 15 jimmason staff 480 Feb 19 2015 gtest1
-rw-r--r-- 1 jimmason staff 282970 Jun 30 2016 gtest1a.groovy
drwxr-xr-x 15 jimmason staff 480 May 7 2016 test2
udemy grails course test
gdrv//_work/jim-acer/blearn/grails/peblock/grails-app/controllers/peblock/UrlMappings.groovy
Grails Install
Grails Setup
cat ./setGrails4.sh
#!/bin/bash
#
echo "----------- "
echo " setGrails4.sh - adds Grails4 home to path"
echo " "
#
# $GRAILS_HOME set
export MYSQL_JDBC=/Users/jimmason/_dev/lib/mysql-connector-java-5.1.34.jar
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
export GRAILS_HOME=/Users/jimmason/_dev/grails4
export PATH=$PATH:$GRAILS_HOME:$GRAILS_HOME/bin
export CLASSPATH=$MYSQL_JDBC:$CLASSPATH
echo $PATH
Grails 3x requires JDK8 not JDK11
verify JDK is JDK8 NOT JDK11 ( the default system JDK I set )
grails --version && groovy --version
Project References
Key Concepts
Grails Training
grails v4x books - authors 4 days
http://docs.grails.org/latest/
grails mvc app using spring services - mode coding
https://www.baeldung.com/grails-mvc-application
https://www.google.com/search?client=firefox-b-1-d&q=grails+version+4+tutorials
Grails and Okta
https://developer.okta.com/blog/2018/04/19/okta-with-grails
Grails CRUD app with Okta
https://developer.okta.com/blog/2018/06/04/okta-with-grails-part2
Grails with Spring Security - default
https://www.slideshare.net/JesusPerezFranco/spring-security-5?from_action=save
Grails download & install
http://grails.org/download.html
Grails Application Forge
Manual Grails install
http://docs.grails.org/latest/guide/gettingStarted.html#downloadingAndInstalling
Set the GRAILS_HOME environment variable to the location where you extracted the zip
On Unix/Linux based systems this is typically a matter of adding something like the following
export GRAILS_HOME=/path/to/grails
to your profile
Then add the bin
directory to your PATH
variable:
On Unix/Linux based systems this can be done by adding
export PATH="$PATH:$GRAILS_HOME/bin"
to your profile
Run version check
- grails version
Create shell script to set Grails version
setGrails41.sh
export GRAILS_HOME=`/Library/Grails/grails-4.1.0.M1`
export PATH=$GRAILS_HOME/bin:$PATH
groovy -version
Grails beta versions
Check for Grails versions released in Github here ( including milestones ) - https://github.com/grails/grails-core/releases
For a given release / milestone - check the Github issues to see what's important that's still open - https://github.com/grails/grails-core/issues
https://github.com/grails/grails-core/releases/tag/v4.1.0.M1
http://docs.grails.org/4.1.0.M1/guide/introduction.html#whatsNew
install from Zip file
ensure GRAILS_HOME, GROOVY_HOME, JAVA_HOME are set
ensure any CLASSPATH and path updates are correct
to validate, run:
grails -version
Grails docs on skwebteam
grails4-update-Slide_Deck_Introducing_Grails_4_Webinar.pdf
grails-181115-tutorial-guestbook-java-web-app-vogella.com-.pdf
gorm-basics-tutorial-relations-maps.pptx
grails-best-practices-web-profile-2016-MarProg_Grails_WP_Web.pdf
grails-web-mvc-tutorial-v4-mongodb-2019-vgood.pdf
grails-plugins.github.io-DB Reverse Engineering Plugin - Reference Documentation.pdf
Grails v4.0 Features
https://objectcomputing.com/products/grails/grails-roadmap
- Groovy 2.5
- GORM 7.0 (Hibernate 5.2 minimum, Java 8 baseline)
- Java 8 Baseline
- Java 11 Support
- Spring 5.1
- Spring Boot 2.1
- Micronaut Integration
Grails JAX-RS plugin documentation ( for Grails v3x )
http://budjb.github.io/grails-jaxrs/3.x/latest/guide/introduction.html
The jaxrs project is a set of Grails plugins that supports the development of RESTful web services based on the Java API for RESTful Web Services (JSR 311: JAX-RS).
It is targeted at developers who want to structure the web service layer of an application in a JSR 311 compatible way but still want to continue to use Grails' powerful features such as GORM, automated XML and JSON marshalling, Grails services, Grails filters and so on. This plugin is an alternative to Grails' built-in mechanism for implementing RESTful web services.
Features
- Makes the JSR 311 (JAX-RS) available to Grails applications for developing RESTful web services.
- New Grails artefact types, Resource and Provider, for JAX-RS classes.
- JAX-RS Resource classes under
grails-app/resources
are auto-detected and can be modified at runtime. - JAX-RS Provider classes under
grails-app/providers
are auto-detected and can be modified at runtime. - Extended Grails command line interface
- Create new resources and unit test templates via
grails create-resource <resource name>
. - Generate ready-to-use resources from domain objects via
grails generate-resources <domain class name>
. - Scaffolding
- Generate RESTful service interfaces for Grails domain objects.
- Content negotiation support for XML and JSON representations.
- Ability to use any Grails feature within JAX-RS resources and providers such as:
- GORM can be used for interacting with persistent domain objects.
- Grails filters for intercepting requests to JAX-RS resources. ( Deprecated )
- Grails services which can be auto-injected by name.
- Entity providers
- Domain object providers that convert between Grails domain objects and XML or JSON representations.
- Support classes for developing custom entity providers.
- Support for content negotiation based on the
Accept
request header. - Easy integration testing of JAX-RS resources and providers.
- Plugin users may choose between Jersey and Restlet as JAX-RS implementations by means of configuration.
- jaxrs applications can be deployed to Google App Engine (GAE).
Tutorial Migrate React Node.js app to Grails backend
https://guides.grails.org/grails-vs-nodejs/guide/index.html
To complete this guide, you will need the following:
Some time on your hands
A decent text editor or IDE
JDK 1.7 or greater installed with
JAVA_HOME
configured appropriately
To get started do the following:
Download and unzip the source
or
Clone the Git repository:
git clone https://github.com/grails-guides/grails-vs-nodejs.git
The Grails guides repositories contain two folders:
initial
Initial project. Often a simple Grails app with some additional code to give you a head-start.complete
A completed example. It is the result of working through the steps presented by the guide and applying those changes to theinitial
folder.
To complete the guide, go to the initial
folder
cd
intograils-guides/grails-vs-nodejs/initial
and follow the instructions in the next sections.
You can go right to the completed example if you The completed sample project for this article can be found at: https://github.com/grails-guides/grails-vs-nodejs/tree/master/complete create react profile appEvery Grails project begins with a single Choose the latest version of Grails (3.3.0 as of the time of writing) and select the
start client and server appsOnce you’ve downloaded your application, expand it into a directory of your choice,
gradle wrapper can eliminate need to install Grails locallyThe gradle is similar to npm for build management It doesn’t provide the CLI that npm offers but it fulfills a similar purpose in dependency management and build-processing. When a Gradle command (or "task") is run, Gradle will first download all dependencies listed in the project’s What about the When running a Gradle “task” from the project root directory, anything after |
Where does bootRun
come from? This Gradle task is inherited from the Spring Boot framework, upon which Grails is based. Of course create-react-app
projects don’t have such a task by default. The React profile provides the client:bootRun
task as a wrapper around the npm/yarn start
script. This allows you to use advanced Gradle features like running both server
and client
in parallel mode with one command. For developers, running ../gradlew client:bootRun
is the same as running npm start
(or yarn start
) in a stock create-react-app
project, and in fact you can run the client
app exactly that way if you have npm
/yarn
installed on your machine.
Once the gradlew
commands have completed downloading dependencies and launching their respective apps, you should be able to browse to http://localhost:8080
to see the Grails backend application, and http://localhost:3000
to view the React app.
Data source setup - postgresql
GORM - Grails Object Relational Management
http://gorm.grails.org/6.1.x/hibernate/manual/#quickStartGuide
A domain class can be created with the create-domain-class
command if you are using Grails, or if you are not using Grails you can just create the .groovy
file manually:
grails create-domain-class helloworld.Person
This will create a class at the location grails-app/domain/helloworld/Person.groovy
such as the one below:
package helloworld
class Person {
}
If you have the configured the dataSource.dbCreate property and set it to "update", "create" or "create-drop", GORM will automatically generate/modify the database tables for you. |
You can customize the class by adding properties:
class Person {
String name
Integer age
Date lastVisit
}
Once you have a domain class try and manipulate it with console
command in Grails by typing:
grails console
This loads an interactive GUI where you can run Groovy commands with access to the Spring ApplicationContext, GORM, etc.
Or if you are not using Grails here is a unit test template (using Spock) that can be run to test out the examples:
import spock.lang.*
import grails.gorm.annotation.Entity
import grails.transaction.Rollback
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
class ExampleSpec extends Specification {
@Shared @AutoCleanup HibernateDatastore hibernateDatastore
@Shared PlatformTransactionManager transactionManager
void setupSpec() {
hibernateDatastore = new HibernateDatastore(Person)
transactionManager = hibernateDatastore.getTransactionManager()
}
@Rollback
void "test execute GORM standalone in a unit test"() {
// your logic here
}
}
@Entity
class Person {
...
}
3.1. Basic CRUD
Try performing some basic CRUD (Create/Read/Update/Delete) operations.
3.1.1. Create
To create a domain class use Map constructor to set its properties and call the save()
method:
def p = new Person(name: "Fred", age: 40, lastVisit: new Date())
p.save()
The save() method will persist your class to the database using the underlying Hibernate ORM layer.
The save()
method is defined by the GormEntity trait.
3.1.2. Read
GORM transparently adds an implicit id
property to your domain class which you can use for retrieval:
def p = Person.get(1)
assert 1 == p.id
This uses the static get(id) method that expects a database identifier to read the Person
object back from the database.
You can also load an object in a read-only state by using the read(id)
method:
def p = Person.read(1)
In this case the underlying Hibernate engine will not do any dirty checking and the object will not be persisted. Note that if you explicitly call the save()
method then the object is placed back into a read-write state.
In addition, you can also load a proxy for an instance by using the load(id)
method:
def p = Person.load(1)
This incurs no database access until a method other than getId() is called. Hibernate then initializes the proxied instance, or throws an exception if no record is found for the specified id.
3.1.3. Update
To update an instance, change some properties and then call save()
again:
def p = Person.get(1)
p.name = "Bob"
p.save()
oks and authors tables - SQL
create table author ( id bigint not auto_increment null, version bigint not null, name varchar(255) not null, email varchar(255) null, primary key (id)) ENGINE=InnoDB; create table book ( id bigint not auto_increment null, version bigint not null, title varchar(255) not null, type varchar(255), description varchar(2048), sold bigint null, primary key (id)) ENGINE=InnoDB; create table author_books ( author_id bigint not null, book_id bigint not null, primary key (author_id, book_id)) ENGINE=InnoDB; alter table author_books add index FKauthor_books_2_books (book_id), add constraint FKauthor_books_2_books foreign key (book_id) references book (id); alter table author_books add index FKauthor_books_2_authors (author_id), add constraint FKauthor_books_2_authors foreign key (author_id) references author(id);
Books and authors output classes from DB plugin
class Author { String name String email String phone static hasMany = [books: Book] }; class Book { String title String publisher BigDecimal price String description Integer quantitySold static hasMany = [authors: Author] static belongsTo = Author }; class Author_Books { String name String email static hasMany = [books: Book] };
Final version of Books and authors output classes
Example of DB Reverse Engineering Output
Yes GORM. The reverse engineering for Grails is a little simpler because all that is needed are Domain classes and the framework handles all the hibernate mappings. So for example a DB table called Application would only need a single Domain class instead of a Domain, DAO and XML Mapping file. Here is an example with one-to-many and many-to-one relationships
class Application { BigDecimal gpa Date submitDate String appliedFlag String acceptedFlag String deniedFlag String managerNetid Date timeStamp AcademicYear academicYear Quarter quarter Student student Set<ApplicationFile> applicationFiles = [] Set<ApplicationQa> applicationQas = [] Set<ApplicationAccepted> applicationAccepteds = [] Set<ApplicationDenied> applicationDenieds = [] static hasMany = [applicationAccepteds: ApplicationAccepted, applicationDenieds: ApplicationDenied, applicationFiles: ApplicationFile, applicationQas: ApplicationQa] static belongsTo = [AcademicYear, Quarter, Student] static mapping = { version false quarter column: "quarter_code" student column: "sid" applicationAccepteds cascade: "save-update, delete-orphan" applicationDenieds cascade: "save-update, delete-orphan" applicationQas cascade: "save-update" } static constraints = { submitDate nullable: true appliedFlag maxSize: 1 acceptedFlag maxSize: 1 deniedFlag maxSize: 1 managerNetid nullable: true, maxSize: 36 } }
There used to be a plugin for Grails that had this functionality and uses the hibernate-tools library but it stopped being maintained a while ago and the newer Grail releases are not compatible. I figured since MyEclipse uses the same hibernate-tools to generate Java code that maybe it would be possible to expand on that to generate Grails domain classes?
http://grails-plugins.github.io/grails-db-reverse-engineer/grails3v4/index.html
Depends on Hibernate Tools, Maven, Eclipse
Golang GORM
GORM - create tables from GOR
- Full-Featured ORM (almost)
- Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism)
- Callbacks (Before/After Create/Save/Update/Delete/Find)
- Preloading (eager loading)
- Transactions
- Composite Primary Key
- SQL Builder
- Auto Migrations
- Logger
- Extendable, write Plugins based on GORM callbacks
- Every feature comes with tests
install
go get -u github.com/jinzhu/gorm
simple crud example
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
// Migrate the schema
db.AutoMigrate(&Product{})
// Create
db.Create(&Product{Code: "L1212", Price: 1000})
// Read
var product Product
db.First(&product, 1) // find product with id 1
db.First(&product, "code = ?", "L1212") // find product with code l1212
// Update - update product's price to 2000
db.Model(&product).Update("Price", 2000)
// Delete - delete product
db.Delete(&product)
}
Generate Golang structs from SQL databases
https://github.com/smallnest/gen
could create a converter to generate GORM domain classes from Golang structs
or
directly create tool to build GORM domain classes from db tables using JDBC metadata and groovy
benefits of direct tool:
- no external dependencies
- could extend to NoSQL
- can pickup reference relations as "has" or "owns"
- can pickup primary key
Grails with Groovy 3x
4.1 M builds use Groovy 3
https://github.com/grails/grails-core/releases/tag/v4.1.0.M1
Potential Value Opportunities
Potential Challenges
Grails Java JDK compatibility
At some later point, ( 2020 or ? ), Grails will catch up with supported Java JDKs but not now.
The low-end default that will work is always JDK 8.
Groovy 2x still works with JDK 8 ( used by Grails 4x ) but not JDK 11 without a lot of custom work.
Grails 4 upgrade issues related to Java etc
https://github.com/grails/grails-core/wiki/Grails-4-Upgrade
Bug> Grails console can't open groovy file from menu or cli
bug> can't open a file from grails console using the file menu
workaround> use CTL + O
- open groovyconsole normally
- use CTL + O to open file selector window
- select a groovy file
- press Enter
- << file opens normally in groovy console
option 1> specify the file name on the cli cmd to open grails console
specify the file to open on the grails console command using relative url
grails console -f ./zfiles/gtest-2a1.groovy
<< no file shown
option 2> manually copy file contents to console window
open grails console
open the file from the File menu ( nothing shows )
open the same groovy file in another text editor
copy and paste the entire contents to the console window
add a comment line
File > save
check the saved file in the text editor to see the comment line exists
Grails 4.1 M++ – Groovy 3 and JDK11
Candidate Solutions
SWT Grails Books and Authors Lab - 2009 - V2X
<< see the pdf for the images >>
Grails lab concepts
The Grails lab uses the c:\ebtnoweos folder to build and run your Grails Web database application.
You will use both the Explorer browser and a DOS cmd shell to generate and run your Grails Web
application.
Before you begin you'll need to start the WebSphere Community Edition server (WAS) that includes
the Derby database you'll use to store you data. You start WAS by clicking wcestart from Explorer.
At the end of the lab click wcestop to end the WAS server.
grails lab flow
• Understand grails datasource configuration
• Start wasce derby server
• Create-app qwex1
• create-domain-class author
• console edit for author
• generate-all for author
• create-domain-class book
• console edit for book
• generate-all for book
• run-app
• open a browser by clicking: http://localhost:9080/qwex1
• create authors
• create books
grails datasource configuration options
The datasource config file defines how grails uses a target database for a specific environment
( development, test or prod )
Sample datasource.groovy config file for the lab
dataSource {
pooled = true
driverClassName = "org.hsqldb.jdbcDriver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:mem:devDB"
}
}
test {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:derby://localhost:1527/sample"
driverClassName = "org.apache.derby.jdbc.ClientDriver"
username = "APP"
password = "APP"
}
}
testh {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:mem:testDb"
}
}
testm {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/sample"
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
}
}
production {
dataSource {
03/14/09 Jim Mason, copyright 2009 3 of 18
ebt-now Enterprise Open-Source Labs
dbCreate = "update"
url = "jdbc:hsqldb:file:prodDb;shutdown=true"
}
}
}
Edit the grails configuration for your data source
When you create your grails application, it generates a default datasource.groovy
config file that does
not include the references for derby and mysql as shown above.
You'll copy the datasource.groovy file
from the savedata folder ( C:\ebtnoweos\savedata\grails)
overlaying the generated version of this file in the
C:\ebtnoweos\grails\qwex1\grails-app\conf folder of
your application.
dbCreate controls how the database tables are maintained from grails
The dbCreate property of the DataSource definition is important as it dictates what Grails should do at runtime with regards to automatically generating the database tables from GORM classes.
The options are:
create-drop - Drop and re-create the database when Grails is run
create - Create the database if it doesn't exist, but don't modify it if it does. Deletes existing data.
update - Create the database if it doesn't exist, and modify it if it does exist
Both create-drop and create will destroy all existing data hence use with caution!
To prevent ANY database changes, comment out dbCreate in the config
In development mode dbCreate is by default set to "create-drop":
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
}
What this does is automatically drop and re-create the database tables on each restart of the application. Obviously this may not be what you want in production or a test server.
Grails – generate Web database application
Start WASCE Derby database server
In Explorer, browse to the root lab folder ( c:\ebtnoweos )
> click wcestart.bat
< WebSphere application server (WAS) starts running after a few minutes
a Dos command window opens with a server startup message
You can open a Web browser on the WAS server by clicking:
Click on the Administrative console to login as the administrator
When prompted for a user id and password,
user = system
password = manager
< the WAS admin console is shown
At the end of the lab, you can click the Shutdown button to shutdown your WAS server
create a Grails application from the cmd line
Click Start > Run > type cmd > Ok
< a DOS cmd shell window opens
Type 'cd \ebtnoweos' to change to the root lab directory
> Type 'setgrails.bat'
< this sets the grails runtime environment
Type 'cd grails'
< you're in the grails app directory
create a grails application: qwex1
At the prompt, Type
'grails create-app qwex1'
< grails generates a basic Web application
change to the application directory
Type 'cd qwex1'
Note: IF you have a grails compile error on any step, always run 'grails test clean' before doing
anything else!
Replace the datasource.groovy config file
The generated datasource.groovy config file is not configured for the Derby database running in WAS CE.
You will copy another datasource.groovy config file replacing the one generated in your application folder.
In Explorer, browse to the C:\ebtnoweos\savedata\grails folder.
Select the datasource.groovy file > right button menu > copy
Browse to the conf folder in your application: C:\ebtnoweos\grails\qwex1\grails-app\conf
> Click on the folder ( not a file )
> right button menu (RBM) > select paste > when prompted say yes to replace the existing file
You've used a new datasource.groovy file that can now connect to the WAS CE Derby database
create an Author domain entity
In the DOS cmd shell, type 'dir' to ensure you are in the grails application folder ( C:\ebtnoweos\grails\
qwex1).
> Type 'grails test create-domain-class author'
< grails generates a Author entity
Edit the Author to add domain attributes
Using grails console, we'll edit the author to add attributes ( fields and methods )
Type 'grails test console'
Grails opens the console in the test environment
> File > Open > select the author.groovy file > Open
< the author.groovy file is loaded into the console editor
You'll add the code shown below in the editor
class Author {
static constraints = {
}
String lastname
String firstname
String email
String phone
}
Do NOT make punctation mistakes on the “ { ) etc
> Click File > Exit to leave the console
< when prompted to save the file, click Yes
The updated Author entity is saved
Generate the Author controller and Web views
Type 'grails test generate-all author'
< grails generates the Author Web views and controller
create a Book domain entity
In the DOS cmd shell, type 'dir' to ensure you are in the grails application folder ( C:\ebtnoweos\grails\
qwex1).
> Type 'grails test create-domain-class book'
< grails generates a Book entity
Edit the Book to add domain attributes
Using grails console, we'll edit the book to add attributes ( fields and methods )
Type 'grails test console'
Grails opens the console in the test environment
> File > Open > select the book.groovy file > Open
< the book.groovy file is loaded into the console editor
You'll add the code shown below in the editor
class Book {
static constraints = {
}
String title
String publisher
BigDecimal price
String publishDate
Author author
}
Do NOT make punctation mistakes on the “ { ) etc
> Click File > Exit to leave the console
< when prompted to save the file, click Yes
The updated Book entity is saved
If the console reopens empty, just click File > Exit
> when prompted to save, click No
You're back at the cmd shell
Generate the Book controller and Web views
Type 'grails test generate-all book'
< grails generates the Book Web views and controller
run your grails application
At the cmd prompt, type 'grails test run-app'
< the internal Jetty application server starts running
wait until you see the server is running message below
Open a Web browser and go to the application URL
< the browser opens to the main page of your application
Edit the Author information as needed
Click the Author list to see the full list of authors
Click Home to return to the main application screen with all the controllers
Edit the Book information as needed
Click the Book Controller
< the Book editor opens
< click New book to create a new book
Notice the first field has a drop down list box to select an author.
In a more advanced lab we'll show you how to display the author's name in the listbox not just the id
The list shows all the books you've entered:
Congratulations – you've built a Web database application in 1 hour!
Simple Grails CRUD app on Grails 2x
User
EBC
Account
Device
Location
Energy
Steps after Grails setup
grails createApp gtcrud1
cd gtcrud1
grails console
- add the application folder to the grails path in the console
Step-by-step guide for Example
sample code block