Grails(2)Clinic Sample and Guide Book
Grails(2)Clinic Sample and Guide Book
Find the most recent sample from here:
https://github.com/grails-samples/grails-petclinic.git
>git clone https://github.com/grails-samples/grails-petclinic.git
Import the project into my STS, install the latest grails on MAC
>sudo port install grails
Follow the document and run the sample first
>grailsw run-app
Then I can visit the sample web page from this URL http://localhost:8080/petclinic.
I can also use my STS web container to run this web project. It is also working. I will go this project in details.
1. PetClinic Application Overview
It is built on already established Java technology like Spring & Hibernate.
Object Relational Mapping(ORM) on Hibernate
View technology called Groovy Server Pages(GSP)
Spring MVC and etc
H2 is the default database used by Grails.
PetClinic Application Design
Logging
http://grails.org/doc/latest/guide/conf.html#logging
grails-app/Config.groovy
Business Layer
org.grails.samples.Speciality
org.grails.samples.PetType
org.grails.samples.Person
org.grails.samples.Vet
org.grails.samples.Owner
org.grails.samples.Pet
org.grails.samples.Visit
Logical Views & Implemented Use Cases
grails-app/views/clinic/index.gsp home
grails-app/views/clinic/vets.gsp vets and specialties
grails-app/views/owner/find.gsp
grails-app/views/owner/selection.gsp select the users from the same last name
grails-app/views/owner/show.gsp
grails-app/views/owner/add.gsp
grails-app/views/pet/add.gsp
grails-app/views/pet/addVisit.gsp
grails-app/views/common/_formField.gsp A template used to render common markup
grails-app/views/layouts/main.gsp The layout used to include common CSS and markup
Testing test the controller layer.
During reading the sample, here is for looking up http://grails.org/doc/latest/guide/index.html.
Make a sample project Dude Money Master
2. Getting Started
I successfully installed grails on my MAC and used the IDE STS.
>grails run-app
>grails test-app
>grails war
4. Configuration
4.1 Basic Configuration
grails-app/conf/BuildConfig.groovy
grails-app/conf/Config.groovy
The syntax is coming from the Grails-ConfigSlurper http://sillycat.iteye.com/blog/1567542.
The configuration has some benefits:
1. Can be merged
2. Can be written to disk
3. Convert to java properties, convert from java properties
4. the content can be structured
5. Can select the environment
def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL())
config =newConfigSlurper("test").parse(newFile('Sample.groovy').toURL())
BuildConfig.groovy is used for compile, doc and etc.
Config.groovy is for settings that are used when the application is running.
So the Config.groovy is packaged in war, BuildConfig.groovy is not.
foo.bar.hello = "world"
It is import to have the quotes. The property values can be any valid Groovy type, strings, integers, or arbitrary objects.
Some implicit variables can not be used:
userHome home directory for the account that is running the grails
grailsHome
appName application name as it appears in application.properties
appVersion version in application.properties
BuildConfig.groovy is only available from command script via grailsSettings.config
Config.groovy is via grailsApplication in controllers
def recipient = grailsApplication.config.foo.bar.hello
And it can be easily injected into services.
class MyService{
def grailsApplication
String greeting(){
def recipient = grailsApplication.config.foo.bar.hello
…snip...
}
}
4.1.1 Built in options
Runtime Settings
grails.config.location The location of properties files can be set to other class path or userHome place.
http://grails.org/doc/latest/guide/single.html#configExternalized
grails.war.dependencies Manage the jars in war.
4.1.2 Logging
The Basics
log4j setting in Config.groovy
Logging Levels
1. off
2. fatal
3. error
4. warn
5. info
6. debug
7. trace
8. all
Loggers
class MyClass {
private static final log = LogFactory.getLog(this)
…snip…
}
The Root Logger
log4j = {
appenders {
file name: 'file', file:'/var/logs/mylog.log'
}
root{
debug 'stdout', 'file'
}
}
The root logger will log to 2 appenders. The default 'stdout' (console) apppender and file appender.
These are the default available appenders.
jdbc JDBCAppender
console ConsoleAppender
file FileAppender
rollingFile RollingFileAppender
Environment-specific configuration
// other shared config
info "grails.app.controller"
environments {
production {
// Override previous setting for 'grails.app.controller'
error "grails.app.controllers"
}
}
4.1.3 GORM
grails.gorm.failOnError=true
grails.gorm.autoFlush=true
4.2 Environments
Per Environment Configuration
dataSource {
pooled = false
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
}
}Packaging and Running for Different Environments
>grails [envrionment] [command name]
references:
http://sillycat.iteye.com/blog/1567542
http://sillycat.iteye.com/blog/1074642
http://www.grails.org/tutorials?offset=0&max=10
https://github.com/grails-samples
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/conf.html