You are previewing Grails 1.1 Web Application Development.
O'Reilly logo
Grails 1.1 Web Application Development

Book Description

Reclaiming Productivity for faster Java Web Development

  • Ideal for Java developers new to Groovy and Grails—this book will teach you all you need to create web applications with Grails

  • Create, develop, test, and deploy a web application in Grails

  • Take a step further into Web 2.0 using AJAX and the RichUI plug-in in Grails

  • Packed with examples and clear instructions to lead you through the development and deployment of a Grails web application

In Detail

Web development is tricky—even a simple web application has a number of context changes ready to trip up the unwary. Grails takes the everyday pain out of web application development, allowing us to focus on delivering real application logic and create seamless experiences that will address the needs of our users. This book will take the pain out of Grails by showing you exactly how to build a web application with a minimum of fuss.

With this book, even if you are new to Grails, you will be up and running before you know it. You will be able to code faster and your code will be better. This clear and concise book is packed with examples and clear instructions to help you build your first Grails application and gives you the skills to speed up your application development by adding a different angle for learning about the topic. After a brief introduction to the dynamic JVM-based Groovy programming language, which teaches you enough about Groovy to understand the relationship between Grails and the Groovy scripting language, it shows how to use Grails and a number of key plug-ins to deliver valuable web applications. It also takes you through creating, developing, testing, and deploying an example team collaboration application in Grails.

Using an incremental and iterative approach you will learn how to build a basic web application with secure authentication and different levels of authorization. You will learn how to handle file upload allowing users to share files. Some advanced features of object-oriented persistence will be introduced through adding tags for messages and files to giving users a robust categorization system.

You will then build on the basic application to enhance the user experience through AJAX and the RichUI plug-in. You will take a further step into the world of Web 2.0 by adding an RSS feed and a REST service to the application. Once the entire application is up and running, you will learn how to create your own plug-in for tagging. Finally, you will learn how to deploy this application to a production environment.

Table of Contents

  1. Grails 1.1 Web Application Development
    1. Table of Contents
    2. Grails 1.1 Web Application Development
    3. Credits
    4. About the author
    5. Acknowledgement
    6. About the reviewers
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code for the book
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started with Grails
      1. Why Grails?
        1. Less configuration
        2. Faster setup
        3. Shorter develop/test cycle
        4. Consistent development environment
        5. Domain-specific language for web development
        6. Fewer dependencies
      2. Installing Grails
      3. Build a team communication portal
      4. Summary
    9. 2. Managing Users with Scaffolding
      1. What is scaffolding?
      2. Create the domain classes
        1. Create the User domain class
        2. Create the Role domain class
      3. Creating controllers to enable scaffolding
      4. Control through constraints
      5. Meeting the relations
        1. Relating roles and users
        2. Ordering fields through constraints
      6. Bootstrapping demo data
      7. Summary
    10. 3. Posting Messages
      1. Message domain class
      2. Rendering a form
        1. Message controller
        2. Groovy Server Pages
        3. Create message view
        4. Grails layouts
        5. Show the form
      3. Handling user input
        1. Binding the request to the domain
        2. Validate and save
        3. Flash scope
        4. Redirect
        5. Render validation errors
        6. Feedback to the user
      4. Create a home page
        1. HomeController
          1. List all messages
        2. Home page view
      5. Styles and navigation
        1. Set the default page
        2. Update the layout
      6. Tidying up
        1. HTML encoding
        2. Overriding validation error messages
      7. Summary
    11. 4. Introduction to Groovy
      1. What is Groovy?
        1. Object-Oriented
        2. Dynamic
        3. Functional
        4. Loosely typed and strongly typed
      2. Why Groovy?
        1. Familiar syntax
        2. Direct integration with Java
      3. Running Groovy
        1. Installing Groovy
        2. Groovy shell
        3. Groovy console
        4. Execute Groovy scripts
        5. Groovy classes and scripts
      4. Groovy features
        1. Semicolons
        2. Strings
        3. Numbers
        4. Lists
        5. Maps
        6. Ranges
        7. Truth and equality
        8. Closures
        9. Plain old Groovy objects
        10. Metaprogramming
          1. Adding dynamic behavior
          2. The great pretender
        11. Builders
      5. Summary
    12. 5. Authentication with JSecurity Plug-in
      1. Where to find plug-ins
      2. Installing a plug-in
      3. Configuring JSecurity plug-in
        1. How does JSecurity work?
        2. More about realms
        3. Create a simple realm
          1. Implement authenticate
          2. Dynamic finders
          3. Implement hasRole
        4. Install the authentication controller
        5. The authentication filter
        6. Password encryption
      4. Add the missing pieces
        1. Encryption of users' passwords
        2. Permission denied page
        3. Sign out link
      5. JSecurity tags
      6. Who posted that?
        1. UserService
        2. Relate messages to users
        3. Making it happen
        4. Showing the user
          1. Hibernate lazy loading
          2. Eager load users
      7. Summary
    13. 6. Testing
      1. Writing unit tests
        1. Why we write unit tests
          1. Confidence in code
          2. Improve design
          3. Developer productivity
          4. Document code
        2. How to write unit tests
          1. Test discreet units of code
          2. Trust your libraries
          3. Test your production code
          4. Descriptive test names
          5. Test one thing at a time
      2. JUnit refresher
        1. SetUp and TearDown
        2. Asserting expectations
      3. Unit tests in Grails
        1. Groovy test extensions
        2. Create a grails test
        3. Running our tests
        4. Grails testing plug-in
          1. Testing Metacontroller
          2. Testing validation
        5. Limitations of Grails in unit tests
      4. Integration testing
        1. External dependencies
        2. Repeatable tests
      5. Integration tests in Grails
        1. BootStrap for environments
      6. Functional testing
        1. Environment setup
        2. Repeatable tests
        3. Test performance
        4. Fragility of tests
        5. Making tests understandable
      7. Functional testing In Grails
        1. Benefits of functional testing in Grails
        2. Installing the functional testing plug-in
      8. Testing guidance
      9. Summary
    14. 7. File Sharing
      1. File domain object
      2. FileController
      3. File Upload GSP
      4. Saving the file
        1. Grails file upload
        2. The save action
        3. Validation messages
      5. Viewing files
      6. Modeling for efficiency
      7. Downloading files
      8. Summary
    15. 8. More GORM and Criteria
      1. More GORM relationships
        1. Sort order of relationships
        2. Update validation messages
      2. Querying with criteria
        1. Comparing criteria and dynamic finders
        2. Using logical operators
        3. Querying across relationships
        4. Specifying a fetch mode for relationships
        5. Criteria reference
        6. Logical criteria
        7. Setting criteria properties
      3. Updating FileController
        1. Updating the create file view
        2. Handling save
      4. Render file versions
      5. Fix file download
      6. Summary
    16. 9. Services
      1. Introducing services
        1. Dependency injection
        2. Service scope
      2. Implementing FileService
      3. Summary
    17. 10. Managing Content through Tagging
      1. Add basic tagging
        1. Tagging domain model
          1. The Tag class
          2. The Tagger class
        2. Tagging a message
        3. Tagging a file
          1. GORM inheritance
            1. Table-per-hierarchy
            2. Table-per-subclass
          2. Taggable superclass
          3. Polymorphic queries
        4. Exposing tagging to the users
          1. Add the Tags input field
          2. Add multiple tags to Taggable
          3. Saving the users tags
          4. Displaying tags
      2. Customize the home page
        1. Introducing templates
        2. Passing data to a template
          1. Render a map
          2. Render an object
          3. Render a collection
        3. Template namespace
        4. Create the message and file templates
        5. User tags
          1. User to tag relationship
          2. User ProfileController
          3. The myTags form
        6. Personalizing the home page
          1. Content service
          2. Update the HomeController
          3. Update the home page
      3. All Messages and Files
      4. Summary
    18. 11. AJAX and RIA Frameworks
      1. Edit tags inline
        1. The remoteLink tag
        2. The formRemote tag
        3. Taggable controller
        4. Tag views
        5. Tagging domain changes
        6. Include the Prototype library
        7. Try it out
      2. Auto-complete tags
        1. Installing the RichUI
        2. Implement the auto-complete
          1. Suggest tags action
          2. RichUI autoComplete widget
      3. Introducing tag clouds
        1. RichUI tag cloud component
        2. Fetch the tag cloud map
        3. Filter by tags
        4. Rendering the tag cloud
      4. Summary
    19. 12. Searching, RSS, and REST Services
      1. Searching
        1. Make our objects searchable
        2. Integrate searching
          1. Site-wide search
          2. Create the search controller
          3. View the search results
          4. Update the templates
            1. Users aren't searchable
        3. More searching
      2. RSS
        1. A note about builders
        2. Create the RSS feed
        3. Remove authorization check
      3. REST services
        1. REST overview
        2. URL mapping
          1. Mapping to variables
          2. Mapping to HTTP response codes
          3. Mapping to HTTP methods
          4. REST service mappings
          5. Mapping to wildcards
          6. Applying constraints
          7. Our Message API mapping
        3. Implementing the RESTful controller
          1. Retrieving messages
          2. Creating new messages
        4. Authentication for REST API
        5. A test script
      4. Summary
    20. 13. Build your own Plug-in
      1. The tagger plug-in
        1. Extract the tagging code
        2. Accessing plug-in templates through Tag Libraries
        3. Calling tags from controllers
        4. Current limitations
        5. Packaging a plug-in
      2. Using plug-in events
        1. Grails plug-in lifecycle events
          1. doWithSpring
          2. doWithWebDescriptor
          3. doWithApplicationContext
          4. doWithDynamicMethods
          5. onChange and onConfigChange
        2. Inspecting Grails artifacts
          1. The GrailsApplication class
          2. Find Taggable domain classes
        3. Re-modeling tag relationships
        4. Adding Taggable behavior
          1. Groovy MetaClass
          2. Getting the home page working
          3. Items of Interest
          4. Create messages and files
          5. Update tags
          6. List messages and files
          7. RSS
      3. Summary
    21. 14. Deployment and the Real World
      1. Set up the environment
        1. Install MySQL
        2. Install Tomcat
      2. Configure Grails
        1. Set up the DataSource
          1. DataSource configuration
          2. Environmentally aware
          3. Database management
          4. Update your DB configuration
          5. Control the bootstrap
        2. Package the WAR file
        3. Deploy
      3. Hosting options
      4. Next steps
        1. Java Hibernate classes with GORM
        2. Integrating with other presentation frameworks
        3. Data migration
      5. Summary
    22. Index