logo资料库

Grails.in.Action.2nd.Edition.2014.6.pdf

第1页 / 共577页
第2页 / 共577页
第3页 / 共577页
第4页 / 共577页
第5页 / 共577页
第6页 / 共577页
第7页 / 共577页
第8页 / 共577页
资料共577页,剩余部分请下载后查看
Front cover
brief contents
contents
foreword
preface
acknowledgments
Glen Smith
Peter Ledbrook
about this book
Roadmap
Who should read this book
Code conventions
Getting the source code
Keeping up to date
Author Online
About the authors
About the technical editor
about the title
about the cover illustration
Part 1—Introducing Grails
1 Grails in a hurry . . .
1.1 Introducing Grails
1.1.1 Why Grails changed the game
1.1.2 Seven big ideas
1.2 Getting set up
1.3 QOTD: your sample program
1.3.1 Writing a controller
1.3.2 Generating an HTML page: the view
1.3.3 Adding style with Grails layouts
1.4 Creating the domain model
1.4.1 Configuring the data source
1.4.2 Exploring database operations
1.5 Adding UI actions
1.5.1 Scaffolding: adding rocket fuel
1.5.2 Surviving the worst-case scenario
1.6 Improving the architecture
1.6.1 Your Grails test case
1.6.2 Going Web 2.0: Ajaxing the view
1.6.3 Bundling the final product: creating a WAR file
1.6.4 And 80 lines of code later
1.7 Summary and best practices
2 The Groovy essentials
2.1 Writing your first script
2.1.1 Using lists, loops, and methods
2.1.2 Working with strings
2.2 Creating a quote analyzer class
2.2.1 Introducing Spock properly
2.2.2 Creating the initial class
2.2.3 Working with maps
2.2.4 Taking the analyzer for a spin
2.3 Going to the next level
2.3.1 Discovering closures
2.3.2 Programming dynamically
2.3.3 To type or not to type
2.4 Summary and best practices
Part 2—Core Grails
3 Modeling the domain
3.1 Introducing the Hubbub sample application
3.1.1 Domain-driven design
3.1.2 Hubbub kick-start: from 0 to first hit
3.1.3 Introducing GORM
3.2 Your first domain class object
3.2.1 Saving and retrieving users via tests
3.2.2 Updating user properties
3.2.3 Deleting users
3.3 Validation: stopping garbage in and out
3.3.1 Standard validators
3.3.2 Custom validation with regular expressions
3.3.3 Cross-field validation tricks
3.3.4 Keeping validation DRY by importing constraints
3.4 Defining the data model—1:1, 1:m, m:n
3.4.1 One-to-one relationships
3.4.2 One-to-many relationships
3.4.3 Many-to-many relationships
3.4.4 Self-referencing relationships
3.5 Summary and best practices
4 Creating the initial UI
4.1 Creating instant UIs with scaffolding
4.1.1 Scaffolding Hubbub’s domain classes
4.1.2 Improving the validation
4.2 Restyling the scaffolding
4.2.1 Changing the skin you’re in
4.2.2 Branding your pages
4.3 Working with the scaffolding code directly
4.3.1 Customizing the dynamic scaffolding
4.3.2 Scaffolding as a starting point
4.4 Summary and best practices
5 Retrieving the data you need
5.1 Setting up the data and search form
5.1.1 Loading sample data
5.1.2 Implementing the search
5.2 Writing Where queries
5.2.1 The query syntax
5.2.2 Exploring Where queries
5.3 When Where queries aren’t suitable
5.3.1 Cheap and cheerful listing and counting
5.3.2 Introducing Criteria queries
5.3.3 Dynamic queries with criteria
5.3.4 Creating a tag cloud using report-style query projections
5.3.5 Using HQL directly
5.4 Summary and best practices
6 Controlling application flow
6.1 Controller essentials
6.2 Implementing a timeline for Hubbub
6.3 Testing controller actions: an introduction to mocking
6.3.1 About unit tests
6.3.2 @TestFor and @Mock mixins
6.3.3 Applying @TestFor and @Mock
6.4 From controller to view
6.4.1 Creating the view
6.4.2 Adding new posts
6.5 Exploring scopes
6.5.1 Request scope
6.5.2 Flash scope
6.5.3 Session scope
6.5.4 servletContext (application) scope
6.6 Handling default actions
6.6.1 One test, two use cases
6.6.2 Working with redirects
6.7 Summary and best practices
7 Services and data binding
7.1 Services: making apps robust and maintainable
7.1.1 Implementing a PostService
7.1.2 Wiring PostService to PostController
7.2 Data binding
7.2.1 Action argument binding
7.2.2 Binding to an existing object
7.2.3 Working with blacklist and whitelist bind params
7.2.4 Complex forms: binding multiple objects
7.2.5 Error handling
7.3 Command objects
7.3.1 Handling custom user registration forms
7.3.2 Participating in injection
7.4 Working with images
7.4.1 Handling file uploads
7.4.2 Uploading to the filesystem
7.4.3 Rendering photos from the database
7.5 Intercepting requests with filters
7.5.1 Writing your first filter
7.5.2 Testing filters
7.5.3 Filter URL options
7.6 Creating custom URL mappings
7.6.1 myHubbub: rolling your own permalinks
7.6.2 Optional variables and constraints
7.6.3 Handling response codes and exceptions
7.6.4 Mapping directly to the view
7.6.5 Wildcard support
7.6.6 Named URL mappings
7.7 Summary and best practices
8 Developing tasty forms, views, and layouts
8.1 Understanding the core form tags
8.1.1 A handful of essential tags
8.1.2 A pocketful of link tags
8.1.3 A tour of the form tags
8.1.4 Adding pagination to the timeline
8.2 Extending views with your own tags
8.2.1 Simple tags
8.2.2 Testing taglibs
8.2.3 Logical tags
8.2.4 Iteration tags
8.2.5 Calling one tag from another
8.3 Adding delicious layouts
8.3.1 Introducing SiteMesh
8.3.2 Standardizing page layouts
8.3.3 Markup fragments with templates
8.3.4 Adding skinning
8.3.5 Implementing navigation tabs
8.4 Applying Ajax tags
8.4.1 Choosing a JavaScript library
8.4.2 Essential Ajax form remoting
8.4.3 Adding sizzle: animation and effects
8.5 Summary and best practices
Part 3—Everyday Grails
9 Building reliable applications
9.1 Running tests
9.1.1 Mastering test execution
9.1.2 Choosing a test phase
9.2 Understanding Grails unit tests and mocks
9.2.1 Mocking core Grails artifacts
9.2.2 Mocking normal collaborators with Spock
9.3 Testing the application as a whole
9.3.1 Introducing browser-based testing with Geb
9.3.2 Understanding how Geb works
9.3.3 Using page objects for maintainability
9.4 Summary and best practices
10 Using plugins: just add water
10.1 Taking advantage of others’ hard work
10.1.1 Finding plugins
10.1.2 Installing plugins via the (deprecated) install-plugin command
10.1.3 Installing plugins via BuildConfig.groovy
10.1.4 Plugin dependencies
10.1.5 Applying your knowledge: the Hubbub extreme makeover begins
10.2 Adding mail support
10.2.1 Sending mail inline
10.2.2 Using a view as your mail body
10.2.3 Testing mail operation
10.3 Caching for performance: making everything snappy
10.3.1 The core caching annotations
10.3.2 Working with the CacheManager API
10.3.3 Leveraging other members of the Cache plugin family
10.3.4 The cache taglibs: caching in the view
10.4 Database migrations: evolving a schema
10.4.1 Installing and configuring the plugin
10.4.2 Establishing a baseline
10.4.3 Implementing common migrations
10.4.4 Groovy-based migrations
10.5 Full-text search: rolling your own search
10.5.1 Making objects searchable
10.5.2 Highlighting hit terms
10.5.3 Implementing pagination
10.5.4 Customizing what gets indexed
10.5.5 Query suggestions: did you mean “Grails”?
10.5.6 Searching across relationships
10.6 Summary and best practices
11 Protecting your application
11.1 Dealing with untrusted data and networks
11.1.1 Validating user input
11.1.2 Data binding
11.1.3 Escaping output
11.1.4 CSRF and form tokens
11.1.5 Protecting your data in transit
11.2 Access control
11.2.1 What is it and what can we use?
11.2.2 Getting started with Spring Security
11.2.3 Protecting URLs
11.2.4 Getting hold of the current user
11.2.5 Using a custom login page
11.2.6 Testing access control
11.3 Further exploration of Spring Security
11.3.1 Tightening restrictions on access
11.3.2 Social authentication
11.4 Summary and best practices
12 Exposing your app to other programs
12.1 Creating a REST interface
12.1.1 What is REST?
12.1.2 Implementing a quick API
12.2 Improving the API
12.2.1 Handling data representations
12.2.2 Customizing the controller
12.2.3 Reporting errors
12.3 Securing and maintaining the API
12.3.1 Configuring API security
12.3.2 Versioning the API
12.3.3 Implementing functional testing
12.4 Summary and best practices
13 Single-page web applications (and other UI stuff)
13.1 Revisiting Grails web resource management
13.1.1 Defining your resources
13.1.2 Using resource modules in your view tier
13.2 RESTful clients with AngularJS
13.2.1 Configuring your Grails app for AngularJS
13.2.2 Your first AngularJS controller: pulling in a RESTful timeline
13.2.3 Creating a new post via REST
13.2.4 Communicating between controllers
13.2.5 Better posting with live UI feedback
13.3 Advanced RESTful CRUD: implementing in-place editing
13.3.1 Implementing UI switching
13.3.2 Introducing an update feature
13.3.3 Finalizing lifecycles with delete
13.4 Summary and best practices
14 Understanding Spring and transactions
14.1 Spring fundamentals
14.1.1 What is dependency injection?
14.1.2 Beans by convention
14.1.3 Customizing an application at runtime
14.2 Using transactions with GORM
14.2.1 Easy transactions with services
14.2.2 Transactions, the session, and me
14.2.3 Fine-grained transactions
14.3 Summary and best practices
Part 4—Advanced Grails
15 Understanding events, messaging, and scheduling
15.1 Lightweight messaging with Platform Core
15.1.1 Installing Platform Core
15.1.2 Sending off an event
15.1.3 Listening for an event
15.1.4 Using namespaces to integrate GORM and events
15.1.5 Aggressive listening: using wildcards
15.1.6 Integrating Spring Security using the grailsEvents bean
15.2 A hitchhiker’s guide to JMS messaging
15.2.1 Learning to think in async: identifying messaging candidates
15.2.2 Messaging terminology: of producers, consumers, queues, and topics
15.2.3 Installing and configuring the JMS plugin
15.3 Using the Grails JMS plugin
15.3.1 Our killer Hubbub feature: IM integration with Jabber
15.3.2 Sending JMS messages
15.3.3 Reading the queue
15.4 Scheduling tasks with Grails
15.4.1 Writing a daily digest job
15.4.2 Fine-grained scheduling with cron
15.5 Advanced scheduling
15.5.1 Dealing with re-entrance and stateful jobs
15.5.2 Pausing and resuming stateful jobs programmatically
15.5.3 Job persistence with JDBS storage
15.6 Summary and best practices
16 NoSQL and Grails
16.1 The problem with PostgreSQL (or when to choose NoSQL)
16.2 Types of NoSQL databases (and typical use cases table)
16.3 Using Redis to work with key-value stores
16.3.1 Installing your own Redis server
16.3.2 Using Redis operations
16.3.3 Installing the Redis plugin (including pooling configuration)
16.3.4 Simple, expiring key/value caching: what is all this @Memoize stuff?
16.3.5 Working with the Redis taglib
16.3.6 Beyond the basics: working with the Redis service object directly
16.3.7 Top posters with Redis sorted sets
16.4 Using MongoDB to work with document-oriented data
16.4.1 Learning MongoDB terminology
16.4.2 Getting set up: installing a MongoDB server
16.4.3 Creating your first database
16.4.4 Installing the MongoDB plugin
16.4.5 Polyglot persistence: Hibernate and MongoDB working together
16.4.6 Stepping outside the schema with embeddables
16.4.7 Dynamic attributes: making up properties as you go along
16.4.8 Querying MongoDB via standard GORM
16.4.9 Working with low-level MongoDB querying
16.5 Using Neo4j to work with graph-oriented data
16.5.1 Installing and configuring the Neo4j plugin
16.5.2 Neo4j domain classes: combining with Hibernate
16.5.3 Populating Hubbub’s social graph
16.5.4 Walking and visualizing the graph with Cypher
16.5.5 Walking the entire graph
16.6 Summary and best practices
17 Beyond compile, test, run
17.1 Getting to deployment
17.1.1 Managing your dependencies
17.1.2 Continuous integration and deployment
17.2 Integrating Grails with Maven
17.2.1 Creating a single-project POM
17.2.2 Multiproject Maven builds
17.3 Grails with Gradle
17.3.1 Building a standalone app
17.3.2 Building a multiproject app
17.4 Summary and best practices
18 Grails in the cloud
18.1 Getting to know the cloud
18.1.1 What is the cloud?
18.1.2 The new kids on the block—PaaS providers
18.2 Running in the cloud
18.2.1 Choosing a cloud provider and assessing Hubbub
18.2.2 Getting familiar with the platform
18.2.3 Adding cache support
18.2.4 Sending emails
18.2.5 Messaging in the cloud with RabbitMQ
18.2.6 Other features to consider
18.3 Summary and best practices
Appendix A—Groovy reference
A.1 Operator overloading
A.2 Groovy JDK methods
Appendix B—GORM query reference
B.1 Where queries
B.2 Criteria queries
Appendix C—XML and Spring builders
C.1 XML generation with MarkupBuilder
C.2 Bean Builder
index
Symbols
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Back cover
SECOND EDITION Glen Smith Peter Ledbrook FOREWORD BY Dierk König M A N N I N G
Grails in Action, Second Edition Licensed to Mark Watson
Licensed to Mark Watson
Grails in Action Second Edition GLEN SMITH PETER LEDBROOK M A N N I N G SHELTER ISLAND Licensed to Mark Watson
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2014 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Development editor: Cynthia Kane Copyeditors: Katie Petito, Lianna Wlasiuk Proofreader: Elizabeth Martin Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617290961 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 19 18 17 16 15 14 Licensed to Mark Watson
To Kylie, who sacrifices daily to let me chase such crazy dreams. Love you so much, matie! —G.S. To my parents, for always being there. —P.L. Licensed to Mark Watson
Licensed to Mark Watson
brief contents PART 1 INTRODUCING GRAILS...................................................1 1 ■ Grails in a hurry . . . 3 2 ■ The Groovy essentials 33 PART 2 CORE GRAILS..............................................................61 3 ■ Modeling the domain 63 4 ■ Creating the initial UI 91 5 ■ Retrieving the data you need 110 6 ■ Controlling application flow 136 7 ■ Services and data binding 155 8 ■ Developing tasty forms, views, and layouts 189 PART 3 EVERYDAY GRAILS .....................................................227 9 ■ Building reliable applications 229 10 ■ Using plugins: just add water 261 11 ■ Protecting your application 297 12 ■ Exposing your app to other programs 328 vii Licensed to Mark Watson
分享到:
收藏