Companion Notes on Using Grails

Russell Bateman
last update: 18 May 2010

I decided to follow a tutorial on Grails by Lars Vogel. As in others he's written, there are a few things to say that he has left somewhat unsaid. This is my attempt to help out the dummies among whom I am the oldest.

In the meantime, I've found what I think may be a better tutorial, the original Mastering Grails: Build your first Grails application by Scott Davis published by IBM. Except that this tutorial does not work, perhaps because it was written for an earlier version of Grails. It's only worth reading.

Nevertheless, ...

Pick up and begin Lars' tutorial at http://www.vogella.de/articles/Grails/article.html.

2. Installation (and) 3. Create Grails web application

Download Grails, create GRAILS_HOME, add GRAILS_HOME/bin to PATH and create first application:

c:\Users\russ\dev> grails create-app de.vogella.grails.guestbook Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev Resolving dependencies... Downloading: C:\Users\russ\dev\grails-1.3.1\lib\gpars-0.9.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\svnkit-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ant-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ant-launcher-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ant-junit-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ant-nodeps-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ant-trax-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jline-0.9.91.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jansi-1.2.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\serializer-2.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-docs-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-bootstrap-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-scripts-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-core-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-resources-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-web-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\slf4j-api-1.5.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\slf4j-log4j12-1.5.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.test-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\servlet-api-2.5.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jsp-api-2.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\groovy-all-1.7.2.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-beanutils-1.8.0.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-el-1.0.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-validator-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\aopalliance-1.0.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-codec-1.3.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-collections-3.2.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-io-1.4.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-lang-2.4.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jta-1.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ejb3-persistence-1.0.2.GA.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\sitemesh-2.4.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-crud-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-gorm-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-spring-1.3.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.core-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.aop-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.aspects-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.asm-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.beans-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.context-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.context.support-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.expression-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.instrument-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.jdbc-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.jms-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.orm-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.oxm-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.transaction-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.web-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\org.springframework.web.servlet-3.0.2.RELEASE.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\aspectjweaver-1.6.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\aspectjrt-1.6.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\cglib-nodep-2.1_3.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-fileupload-1.2.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\oro-2.0.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jstl-1.1.2.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-dbcp-1.2.2.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\commons-pool-1.5.3.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\hsqldb-1.8.0.10.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\ehcache-core-1.7.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\log4j-1.2.15.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jcl-over-slf4j-1.5.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\jul-to-slf4j-1.5.8.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\standard-1.1.2.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\xpp3_min-1.1.3.4.O.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\junit-4.8.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\dist\grails-test-1.3.1.jar ... Download complete. Dependencies resolved in 1908ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\CreateApp_.groovy Environment set to development [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\src [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\src\java [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\src\groovy [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\controllers [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\services [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\domain [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\taglib [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\utils [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\views [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\views\layouts [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\i18n [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\conf [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\test [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\test\unit [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\test\integration [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\scripts [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\web-app [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\web-app\js [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\web-app\css [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\web-app\images [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\web-app\META-INF [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\lib [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\conf\spring [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\conf\hibernate [propertyfile] Creating new property file: C:\Users\russ\dev\de.vogella.grails.guestbook\application.properties [copy] Copying 1 resource to C:\Users\russ\dev\de.vogella.grails.guestbook [unjar] Expanding: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-shared-files.jar into C:\Users\russ\dev\de.vogella.grails.guestbook [delete] Deleting: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-shared-files.jar [copy] Copying 1 resource to C:\Users\russ\dev\de.vogella.grails.guestbook [unjar] Expanding: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app-files.jar into C:\Users\russ\dev\de.vogella.grails.guestbook [delete] Deleting: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app-files.jar [copy] Copying 1 resource to C:\Users\russ\.grails\1.3.1\projects\dev\integration-files [unjar] Expanding: C:\Users\russ\.grails\1.3.1\projects\dev\integration-files\grails-integration-files.jar into C:\Users\russ\.grails\1.3.1\projects\dev\integration-files [delete] Deleting: C:\Users\russ\.grails\1.3.1\projects\dev\integration-files\grails-integration-files.jar [copy] Copying 3 files to C:\Users\russ\dev\de.vogella.grails.guestbook Created Eclipse project files. Installing zip C:\Users\russ\dev\grails-1.3.1\plugins\grails-hibernate-1.3.1.zip... ... [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins\hibernate-1.3.1 [unzip] Expanding: C:\Users\russ\dev\grails-1.3.1\plugins\grails-hibernate-1.3.1.zip into C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins\hibernate-1.3.1 Installed plugin hibernate-1.3.1 to location C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins/hibernate-1.3.1. ... Resolving plugin JAR dependencies ... Downloading: C:\Users\russ\dev\grails-1.3.1\lib\hibernate-core-3.3.1.GA.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\hibernate-commons-annotations-3.1.0.GA.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\hibernate-annotations-3.4.0.GA.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\javassist-3.11.0.GA.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\antlr-2.7.6.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\dom4j-1.6.1.jar ... Download complete. Downloading: C:\Users\russ\dev\grails-1.3.1\lib\hibernate-ehcache-3.3.1.GA.jar ... Download complete. Executing hibernate-1.3.1 plugin post-install script ... Plugin hibernate-1.3.1 installed Installing zip C:\Users\russ\dev\grails-1.3.1\plugins\grails-tomcat-1.3.1.zip... ... [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins\tomcat-1.3.1 [unzip] Expanding: C:\Users\russ\dev\grails-1.3.1\plugins\grails-tomcat-1.3.1.zip into C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins\tomcat-1.3.1 Installed plugin tomcat-1.3.1 to location C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugins/tomcat-1.3.1. ... Executing tomcat-1.3.1 plugin post-install script ... Plugin tomcat-1.3.1 installed Plugin provides the following new scripts: ------------------------------------------ grails tomcat Created Grails Application at C:\Users\russ\dev/de.vogella.grails.guestbook

This creates a new filesystem for the project/application:

c:\Users\russ\dev> tree de.vogella.grails.guestbook Folder PATH listing Volume serial number is BE79-0AEB C:\USERS\RUSS\DEV\DE.VOGELLA.GRAILS.GUESTBOOK +---.settings +---grails-app � +---conf � � +---hibernate � � +---spring � +---controllers � +---domain � +---i18n � +---services � +---taglib � +---utils � +---views � +---layouts +---lib +---scripts +---src � +---groovy � +---java +---test � +---integration � +---unit +---web-app +---css +---images � +---skin +---js � +---prototype +---META-INF +---WEB-INF +---tld

3.2. Run application

Note that you might not be able to run your Grails application if you've got an instance of Eclipse up.

c:\Users\russ\dev> cd de.vogella.grails.guestbook c:\Users\russ\dev\de.vogella.grails.guestbook> grails run-app Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 735ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\RunApp.groovy Environment set to development [copy] Copied 3 empty directories to 2 empty directories under C:\Users\russ\.grails\1.3.1\projects\de.vogella.grai ls.guestbook\resources [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugin-classes [groovyc] Compiling 2 source files to C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugin-classes [mkdir] Created dir: c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 7 source files to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\resources\grails-app\i18n [native2ascii] Converting 13 files from C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\i18n to C:\Users\russ\. grails\1.3.1\projects\de.vogella.grails.guestbook\resources\grails-app\i18n [copy] Copying 1 file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [copy] Copied 2 empty directories to 2 empty directories under C:\Users\russ\.grails\1.3.1\projects\de.vogella.grai ls.guestbook\resources Running Grails application.. Server running. Browse to http://localhost:8080/de.vogella.grails.guestbook

Go to the indicated URL in a browser (click link above).

Stop the server

Stop the server (press ^C).

Application context shutting down... Application context shutdown. Terminate batch job (Y/N)? y c:\Users\russ\dev\de.vogella.grails.guestbook>

3.4. Create domain model

Add the domain mode so the application actually does something. We'll have the notion of feedback, a user and his or her comments.

c:\Users\russ\dev\de.vogella.grails.guestbook> grails create-domain-class de.vogella.grails.guestbook.Feedback Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 720ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\CreateDomainClass.groovy Environment set to development [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\domain\de\vogella\grails\guestbook Created DomainClass for Feedback [mkdir] Created dir: C:\Users\russ\dev\de.vogella.grails.guestbook\test\unit\de\vogella\grails\guestbook Created Tests for Feedback c:\Users\russ\dev\de.vogella.grails.guestbook> grails create-domain-class de.vogella.grails.guestbook.User Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 805ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\CreateDomainClass.groovy Environment set to development Created DomainClass for User Created Tests for User c:\Users\russ\dev\de.vogella.grails.guestbook> grails create-domain-class de.vogella.grails.guestbook.Comment Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 810ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\CreateDomainClass.groovy Environment set to development Created DomainClass for Comment Created Tests for Comment

Now look at the application filesystem closer. You'll notice that it strongly resembles an Eclipse project by reason of its subdirectories and the presence of .classpath, .project and .settings.

c:\Users\russ\dev\de.vogella.grails.guestbook> dir Volume in drive C has no label. Volume Serial Number is BE79-0AEB Directory of c:\Users\russ\dev\de.vogella.grails.guestbook 05/17/2010 09:44 AM <DIR> . 05/17/2010 09:44 AM <DIR> .. 05/17/2010 09:38 AM 766 .classpath 05/17/2010 09:38 AM 508 .project 05/17/2010 09:38 AM <DIR> .settings 05/17/2010 09:38 AM 207 application.properties 05/17/2010 06:46 AM <DIR> grails-app 05/17/2010 09:38 AM <DIR> lib 05/17/2010 09:38 AM <DIR> scripts 05/17/2010 09:38 AM <DIR> src 05/17/2010 09:44 AM <DIR> target 05/17/2010 09:38 AM <DIR> test 05/17/2010 06:46 AM <DIR> web-app 3 File(s) 1,481 bytes 10 Dir(s) 445,964,414,976 bytes free

Groovy source code files are extended by .groovy. You'll find the new classes coded underneath the grails-app/domain subdirectory. Add code to these classes per the tutorial.

When you change anything in the domain, you'll need to drop the Grails server and run the grails clean.

c:\Users\russ\dev\de.vogella.grails.guestbook> grails clean Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 754ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\Clean.groovy Environment set to development [delete] Deleting: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\resources\web.xml [delete] Deleting directory c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [delete] Deleting directory C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugin-classes [delete] Deleting directory C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\resources

Now set up to use dynamic scaffolding via these commands:

c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-controller de.vogella.grails.guestbook.Feedback Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 711ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateController.groovy Environment set to development [copy] Copied 3 empty directories to 2 empty directories under C:\Users\russ\.grails\1.3.1\projects\de.vogella.grai ls.guestbook\resources [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugin-classes [groovyc] Compiling 2 source files to C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\plugin-classes [mkdir] Created dir: c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 10 source files to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [mkdir] Created dir: C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\resources\grails-app\i18n [native2ascii] Converting 13 files from C:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\i18n to C:\Users\russ\. grails\1.3.1\projects\de.vogella.grails.guestbook\resources\grails-app\i18n [copy] Copying 1 file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [copy] Copied 2 empty directories to 2 empty directories under C:\Users\russ\.grails\1.3.1\projects\de.vogella.grai ls.guestbook\resources [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes Generating controller for domain class de.vogella.grails.guestbook.Feedback ... Created Tests for Feedback Finished generation for domain class de.vogella.grails.guestbook.Feedback c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-controller de.vogella.grails.guestbook.User Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 808ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateController.groovy Environment set to development [groovyc] Compiling 2 source files to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes Generating controller for domain class de.vogella.grails.guestbook.User ... Created Tests for User Finished generation for domain class de.vogella.grails.guestbook.User c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-controller de.vogella.grails.guestbook.Comment Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 915ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateController.groovy Environment set to development [groovyc] Compiling 2 source files to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes Generating controller for domain class de.vogella.grails.guestbook.Comment ... Created Tests for Comment Finished generation for domain class de.vogella.grails.guestbook.Comment

This creates controller classes which, by definition, are named classnameController

c:\Users\russ\dev\de.vogella.grails.guestbook> dir grails-app\controllers\de\vogella\grails\guestbook Volume in drive C has no label. Volume Serial Number is BE79-0AEB Directory of c:\Users\russ\dev\de.vogella.grails.guestbook\grails-app\controllers\de\vogella\grails\guestbook 05/17/2010 10:47 AM <DIR> . 05/17/2010 10:47 AM <DIR> .. 05/17/2010 10:47 AM 4,035 CommentController.groovy 05/17/2010 10:47 AM 4,099 FeedbackController.groovy 05/17/2010 10:47 AM 3,843 UserController.groovy 3 File(s) 11,977 bytes 2 Dir(s) 445,963,411,456 bytes free

3.5. Dynamic Scaffolding

Add code to the generated controllers. The verbal instructions are extremely misleading. In fact, you don't replace anything or line at all. Instead, you insert def scaffold = true at the top of each class. The only way you know this is because the displayed code for FeedbackController has a comment to this effect.

Re-run the application and browse to http://localhost:8080/de.vogella.grails.guestbook as before.

c:\Users\russ\dev\de.vogella.grails.guestbook> grails run-app Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 804ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\RunApp.groovy Environment set to development [groovyc] Compiling 4 source files to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [delete] Deleting directory C:\Users\russ\.grails\1.3.1\projects\de.vogella.grails.guestbook\tomcat Running Grails application.. Server running. Browse to http://localhost:8080/de.vogella.grails.guestbook

The way the application works is you click on one of the controllers to create a new object. Since you can't create feedback, for instance, without a user, you must first add a user, then you can add feedback.

Note: if you drop the application (but not the server) then restart it, you data is still there.

3.6. Bootstrapping

Add the code suggested and run Grails again. There will be two users and some other data already in the application as if it had been entered.

3.7. Store data

As noted, except that I never got the errors the tutorial said I would. Instead, Grails merely duplicated the users:

(I never removed the bootstrapping code.)

3.8. Static scaffolding (and views)

I got an error doing this for User, and not the error the tutorial warned of (since, for sure, our JAVA_HOME is mapped to a Sun JDK and not a mere JRE).

c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-views de.vogella.grails.guestbook.Feedback Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 828ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateViews.groovy Environment set to development [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes Generating views for domain class de.vogella.grails.guestbook.Feedback ... Finished generation for domain class de.vogella.grails.guestbook.Feedback c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-views de.vogella.grails.guestbook.User Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 869ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateViews.groovy Environment set to development [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: [groovyc] c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes\resources$_run_closure1.class (The \ requested operation cannot be performed on a file with a user-mapped section open) [groovyc] [groovyc] 1 error Compilation error: Compilation Failed c:\Users\russ\dev\de.vogella.grails.guestbook> grails generate-views de.vogella.grails.guestbook.Comment Welcome to Grails 1.3.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\russ\dev\grails-1.3.1 Base Directory: C:\Users\russ\dev\de.vogella.grails.guestbook Resolving dependencies... Dependencies resolved in 852ms. Running script C:\Users\russ\dev\grails-1.3.1\scripts\GenerateViews.groovy Environment set to development [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes [groovyc] Compiling 1 source file to c:\Users\russ\dev\de.vogella.grails.guestbook\target\classes Generating views for domain class de.vogella.grails.guestbook.Comment ... Finished generation for domain class de.vogella.grails.guestbook.Comment

Running afterward, I detected a problem when I clicked on the User controller. The tutorial doesn't explain what a "user-mapped section" is or why what we've done this far, especially since we didn't write the code, would leave it "open".


Note: This said, when I re-tried the tutorial, using the name guestbook instead of de.vogella.grails.guestbook, I did not get the error. (It has nothing to do with the name; I probably screwed something up editing one of the files.)

(I commented out def scaffold = true; form the three controller classes.)

4.1. Changing the generated views

As noted before this section, the files to modify are on the path grails-app/views/feedback/*.gsp. They are create.gsp, edit.gsp, list.gsp and show.gsp.

I think this one is create.gsp. The reason this is in question is probably because of changes to Grails that have internationalized the .gsp files. Therefore, things like:

<title>Create Feedback</title>

...are now:

<title><g:message code="default.create.label" args="[entityName]" /></title>

Similarly, you'll find the views for comments, user (if you can go back and fix that problem) and feedback as noted under their respective subdirectories.

You can find and modify what gets displayed on the "main" page under the layout subdirectory.

7. Using Eclipse for Grails development

http://dist.springsource.org/release/GRECLIPSE/e3.5/

I found that installing the software would not work because after agreeing to the end-user license agreement, neither Next nor Finish would appear. Backing up and re-doing the installation, but not choosing Next, but Finish early on (by-passing the EULA?) seemed to go down another path and not hang on the EULA.

After installation and import of project into Eclipse, I found that the unfleshed out Spring portions of the project gave errors (for not being filled out) and that there isn't any file on which you can right-click and choose Run As that leads to executing the project.