Grails(2)Clinic Sample and Guide Book

AllenYoung 2012-12-18

Grails(2)ClinicSampleandGuideBook

Findthemostrecentsamplefromhere:

https://github.com/grails-samples/grails-petclinic.git

>gitclonehttps://github.com/grails-samples/grails-petclinic.git

ImporttheprojectintomySTS,installthelatestgrailsonMAC

>sudoportinstallgrails

Followthedocumentandrunthesamplefirst

>grailswrun-app

ThenIcanvisitthesamplewebpagefromthisURLhttp://localhost:8080/petclinic.

IcanalsousemySTSwebcontainertorunthiswebproject.Itisalsoworking.Iwillgothisprojectindetails.

1.PetClinicApplicationOverview

ItisbuiltonalreadyestablishedJavatechnologylikeSpring&Hibernate.

ObjectRelationalMapping(ORM)onHibernate

ViewtechnologycalledGroovyServerPages(GSP)

SpringMVCandetc

H2isthedefaultdatabaseusedbyGrails.

PetClinicApplicationDesign

Logging

http://grails.org/doc/latest/guide/conf.html#logging

grails-app/Config.groovy

BusinessLayer

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

LogicalViews&ImplementedUseCases

grails-app/views/clinic/index.gsphome

grails-app/views/clinic/vets.gspvetsandspecialties

grails-app/views/owner/find.gsp

grails-app/views/owner/selection.gspselecttheusersfromthesamelastname

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.gspAtemplateusedtorendercommonmarkup

grails-app/views/layouts/main.gspThelayoutusedtoincludecommonCSSandmarkup

Testingtestthecontrollerlayer.

Duringreadingthesample,hereisforlookinguphttp://grails.org/doc/latest/guide/index.html.

MakeasampleprojectDudeMoneyMaster

2.GettingStarted

IsuccessfullyinstalledgrailsonmyMACandusedtheIDESTS.

>grailsrun-app

>grailstest-app

>grailswar

4.Configuration

4.1BasicConfiguration

grails-app/conf/BuildConfig.groovy

grails-app/conf/Config.groovy

Theconfigurationhassomebenefits:

1.Canbemerged

2.Canbewrittentodisk

3.Converttojavaproperties,convertfromjavaproperties

4.thecontentcanbestructured

5.Canselecttheenvironment

defconfig=newConfigSlurper("development").parse(newFile('Sample.groovy').toURL())

config=newConfigSlurper("test").parse(newFile('Sample.groovy').toURL())

BuildConfig.groovyisusedforcompile,docandetc.

Config.groovyisforsettingsthatareusedwhentheapplicationisrunning.

SotheConfig.groovyispackagedinwar,BuildConfig.groovyisnot.

foo.bar.hello="world"

Itisimporttohavethequotes.ThepropertyvaluescanbeanyvalidGroovytype,strings,integers,orarbitraryobjects.

Someimplicitvariablescannotbeused:

userHomehomedirectoryfortheaccountthatisrunningthegrails

grailsHome

appNameapplicationnameasitappearsinapplication.properties

appVersionversioninapplication.properties

BuildConfig.groovyisonlyavailablefromcommandscriptviagrailsSettings.config

Config.groovyisviagrailsApplicationincontrollers

defrecipient=grailsApplication.config.foo.bar.hello

Anditcanbeeasilyinjectedintoservices.

classMyService{

defgrailsApplication

Stringgreeting(){

defrecipient=grailsApplication.config.foo.bar.hello

…snip...

}

}

4.1.1Builtinoptions

RuntimeSettings

grails.config.locationThelocationofpropertiesfilescanbesettootherclasspathoruserHomeplace.

http://grails.org/doc/latest/guide/single.html#configExternalized

grails.war.dependenciesManagethejarsinwar.

4.1.2Logging

TheBasics

log4jsettinginConfig.groovy

LoggingLevels

1.off

2.fatal

3.error

4.warn

5.info

6.debug

7.trace

8.all

Loggers

classMyClass{

privatestaticfinallog=LogFactory.getLog(this)

…snip…

}

TheRootLogger

log4j={

appenders{

filename:'file',file:'/var/logs/mylog.log'

}

root{

debug'stdout','file'

}

}

Therootloggerwilllogto2appenders.Thedefault'stdout'(console)apppenderandfileappender.

Thesearethedefaultavailableappenders.

jdbcJDBCAppender

consoleConsoleAppender

fileFileAppender

rollingFileRollingFileAppender

Environment-specificconfiguration

//othersharedconfig

info"grails.app.controller"

environments{

production{

//Overrideprevioussettingfor'grails.app.controller'

error"grails.app.controllers"

}

}

4.1.3GORM

grails.gorm.failOnError=true

grails.gorm.autoFlush=true

4.2Environments

PerEnvironmentConfiguration

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"

}

}

}PackagingandRunningforDifferentEnvironments

>grails[envrionment][commandname]

references:

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

相关推荐