Grails 1.1 Web
Application
Development
Reclaiming Productivity
for Faster
Java Web
Development
Jon Dickinson
PUBLISHING J
Table
of
Contents
Preface
1_
Chapter
1:Getting
Started with Grails 7Why
Grails? 7Less
configuration
8Faster
setup
8Shorter
develop/test cycle
9 Consistentdevelopment
environment 9Domain-specific
language
for webdevelopment
10Fewer
dependencies
10Installing
Grails 11Build a team communication
portal
14Summary
15Chapter
2:Managing
Users withScaffolding
17What is
scaffolding?
17 Createthe domain classes 18Create the User domain class 19
Create the Roledomain class 20
Creating
controllers to enablescaffolding
21Control
through
constraints 25Meeting
the relations 29Relating
roles and users 29Ordering
fieldsthrough constraints 31Bootstrapping
demo data 31Summary
33Chapter
3:Posting Messages
35Message
domain class 36Rendering
a form 37TabicofContents
Groovy
ServerPages Create message viewGrailslayouts
Show the form
Handling userinput
Binding the requestto thedomain
Validate and save
Flash scope Redirect
Render validation errors
Feedbackto theuser
Createa home page
HomeController
List all messages
Home page view
Styles
andnavigation
Setthe default pageUpdate the layout
Tidying up HTMLencoding
Overriding
validation error messagesSummary
Chapter 4: Introduction to Groovy
What is
Groovy?
59Object-Oriented
60Dynamic
60Functional 61
Loosely typed and strongly typed 61
Why Groovy? 62
Familiar syntax 62
Direct integration with Java 62
Running Groovy
63Installing Groovy
63Groovy
shell 65Groovy
console 66Execute Groovy scripts 66
Groovy classes and scripts 67
GroovyFeatures 68 Semicolons 68
Strings
69 Numbers 70 37 38 39 39 41 42 43 44 44 45 46 48 48 48 49 52 52 52 54 55 57 58 59TableofContents
Lists 72
Maps 74
Ranges
75Truth and equality 76
Closures 77
Plain old
Groovy objects
78Metaprogramming
79Adding dynamicbehavior 80
The great pretender
80
Builders 81
Summary
83Chapter
5: Authentication withJSecurity
Plug-in
85 Where tofindplug-ins
86Installing
a plug-in 86Configuring JSecurity plug-in
88How does
JSecurity
work? 88More aboutrealms 89
Create a
simple
realm 90Implementauthenticate 91
Dynamicfinders 92
Implement hasRole 94
Install the authentication controller 94
The authenticationfilter 95
Password
encryption
97Add the
missing
pieces 99Encryption
of users'passwords
99Permission denied page 101
Sign out link 103
Who posted that? 105
Relate messages to users 106
Making
ithappen
107Showing
the user 108Hibernate lazy loading 109
Eagerload users 110
Summary
111Chapter 6:
Testing
113
Writing
unit tests 114Why wewrite unittests 114
Confidencein code 114
Improve design 114
Developerproductivity 114
TableofContents
How towriteunit tests 115
Test discreet units of code H5
Trust your libraries 115
Test yourproductioncode 116
Descriptivetestnames 116
Testonethingatatime 117
JUnit refresher 117
SetUp and TearDown 118
Asserting expectations 118
Unittests in Grails 119
Groovytestextensions 119
Create a
grails
test 121Running
our tests 121Grailstesting
plug-in
122Testing Metacontroller 123
Testingvalidation 127
Limitations of Grails in unit tests 128
Integration testing 128
External
dependencies
129Repeatable
tests 129Integration
tests in Grails 129Bootstrap
for environments 130Functional testing 132
Environment setup 132
Repeatable
tests 133Test performance 133
Fragility of tests 133
Makingtests understandable 133
Functional
testing
In Grails 134Benefits of functionaltesting in Grails 134
Installing
thefunctionaltesting plug-in 134Testing guidance
136Summary 137
Chapter
7: File Sharing 139File domain
object
139FileController 140
File Upload GSP 141
Saving
the file 143Grails file
upload
143Thesave action 143
Validation messages 145
TableofContents
Modeling
forefficiency
147Downloading
files 149Summary
150Chapter
8: More GORM and Criteria 151Sort order of
relationships
153Update
validation messages 153Querying
with criteria 153Comparing
criteria anddynamic
finders 154Using
logical
operators 155Querying
acrossrelationships
155Specifying
afetchmode forrelationships
156Criteria reference 156
Logical
criteria 158Setting
criteria properties 158Updating
FileController 159Updating
the create file view 159Handling
save 160Render file versions 161 Fixfiledownload 164
Summary
164Chapter
9: Services 165Introducing
services 165Dependency injection
166 Service scope 167Implementing
FileService 168Summary
172Chapter
10:Managing
Contentthrough Tagging
173Add basic
tagging
173Tagging
domain model 174TheTag class 174
TheTaggerclass 174
Tagging
a message 176Tagging
afile 178GORM inheritance 179
Taggable superclass 181
Polymorphic queries 182
Exposing
tagging
to the users 185Add theTags inputfield 185
Addmultiple tagstoTaggable 186
Savingthe userstags 186
Displaying tags 187
TableofContents
Customize the home page 189
Introducing
templates 189Passing
data to atemplate
190Renderamap "191
Renderanobject "191
Renderacollection 191
Template
namespace 192Create the message and file templates 192
User
tags
193Usertotag relationship 193
User ProfileController 194
ThemyTagsform 195
Personalizing the home page 196
Content service 196
UpdatetheHomeController 199
Updatethe home page 199
All Messages and Files 201
Summary
204Chapter 11: AJAX and RIA Frameworks 205
Edittags inline 205
The remoteLinktag 206
The formRemote tag 207
Taggable controller 208
Tag
views 209Tagging
domainchanges
211Include the
Prototype library
211Try
itout 212Auto-complete
tags 213Installing the RichU I 213
Implementthe
auto-complete
214Suggest tagsaction 214
RichUI autoComplete widget 215
Introducing
tag clouds 219RichUI tag cloud
component
219Fetch thetag cloud map 221
Filterby tags 221
Rendering
thetag cloud 224Summary
225Chapter
12:Searching,
RSS, and REST Services 227Searching
227Make our
objects
searchable 228Tableof Contents
Site-wide search 229
Create the search controller 230
Viewthe search results 231
Updatethetemplates 232
More
searching
234RSS 235
A note about builders 235
Create the RSS feed 236
Remove authorization check 238
REST services 239
REST overview 239
URL mapping 240
Mapping to variables 241
Mapping toHTTP responsecodes 241
Mapping toHTTP methods 241
REST servicemappings 242
Mapping to wildcards 242
Applying constraints 242 OurMessageAPI mapping 243
Implementing
the RESTful controller 243Retrieving messages 244
Creatingnew messages 245
Authentication for RESTAPI 247
A test
script
248Summary
250Chapter
13: Build Your Own Plug-in 251Thetagger
plug-in
251Extractthetagging code 253
Accessing
plug-in templatesthrough
Tag Libraries 253Calling
tagsfrom controllers 255Current limitations 256
Packaging
aplug-in
256Using plug-in
events 257Grails plug-in
lifecycle
events 257doWithSpring 258
doWithWebDescriptor 259
doWithApplicationContext 259
doWithDynamicMethods 259
onChange andonConfigChange 259
Inspecting
Grailsartifacts 260TheGrailsApplication class 260
Find Taggabledomain classes 261
Re-modeling
tagrelationships
262TableofContents
GroovyMetaClass 263 Gettingthe homepageworking
Items ofInterest
^70
Create messagesandfiles 272
Updatetags 274
List messagesandfiles 1'°
RSS 280
Summary 280
Chapter14:
Deployment
and the Real World 283 Set up the environment 283Install
MySQL
283Install Tomcat 284
Configure Grails 285
Set up theDataSource 285
DataSourceconfiguration 285
Environmentallyaware 286
Database management 287
UpdateyourDB configuration 288
Control thebootstrap 289
Package
the WAR file 291Deploy 292
Hosting options 292
Nextsteps 292
Java Hibernate classeswith GORM 292
Integrating
with otherpresentation frameworks 293Data migration 294