Grails
High-velocity development for Spring applications
About Me
David Jacobs
● Web Developer since '98

● Groovy since '06          Twitter
                            @MetaThis...
What is Grails?
Grails is an open-source framework for high-velocity
development of Spring applications
 ● Sensible defaul...
Best-of-Breed Java Technologies
●   Spring
●   Spring MVC
●   Hibernate
●   log4j
●   jUnit
●   SiteMesh
Integrated Stack
●   Layers and technologies integrated and wired
    out-of-the-box
●   Default configurations based on i...
Directory Structure
●   grails-app - top level directory for Groovy sources
     ●   conf - Configuration sources.
     ● ...
GORM
●   Object Relational Mapping (ORM)
●   Data access layer
●   Simplifies configuration through conventions
●   Defaul...
GORM
Domain Modeling
//this is a complete Hibernate mapping!
class Employee {
    String firstName
    String lastName
   ...
GORM
Dynamic CRUD
def employee = Employee.get(1)
employee.delete()
def newEmployee = new Employee(firstName: “Joe”, lastNa...
GORM
Dynamic finders
Employee.findByLastNameAndHireDateGreaterThan(“Jones”, someDate)


findBy and findAllBy
 ●   InList -...
GORM
Hibernate HQL
Employee.findAll("from Employee as e where e.lastName like :lastName", [lastName:"Jon%"])

//with pagin...
GORM
Simplified Hibernate Criteria API
// Find incomplete tasks assigned to Jones where the company is Monsanto
// and the...
Web Layer
●   Controllers built on Spring MVC
●   URL mapping conventions map requests to controllers
●   Naming and direc...
Spring MVC
Request parameters parsed into multidimensional params map
     ●   Easily accessed with powerful Groovy map su...
Data Binding
def save = {
    //bind params to new instance
    def user = new User(params)

    //persist
    user.save()...
Request Format Transparency
Grails codecs (dynamic encode/decode methods) easily automap formats like XML and JSON to
the ...
XML & JSON Marshalling
Groovy's popular XML support
+
Grails builders and codecs
+
Convenience methods

def list = {
    r...
Groovy Server Pages (GSP)
●   Similar to JSP
     ●   Tag library like JSTL, but much more powerful
     ●   Easy custom t...
Groovy Server Pages (GSP)
Tags
   actionSubmit     fieldValue      layoutBody       render
   applyLayout      findAll    ...
AJAX
●   Bundled with Prototype and Script.aculo.us
●   Excellent jQuery plugin
●   GSP tags for AJAX
     ●   Adapters bu...
Configuration
Per-Environment Configuration Supports SCM
    ●    Package or run as any configured environment. Default en...
Plugins
Grails extensions are provided as plugins
 ●   Created by core Grails team and community
 ●   Powerful – a plugin ...
Plugins
Plugin development is very active
The Bottom Line
●   Increases developer productivity
●   less code + less configuration = easier to maintain
●   Smaller l...
Upcoming SlideShare
Loading in...5
×

Grails 101

1,961

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,961
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Grails 101"

  1. 1. Grails High-velocity development for Spring applications
  2. 2. About Me David Jacobs ● Web Developer since '98 ● Groovy since '06 Twitter @MetaThis ● Grails focus in '08-'09
  3. 3. What is Grails? Grails is an open-source framework for high-velocity development of Spring applications ● Sensible defaults ● Convention-over-configuration ● DRY ● Dynamic metaprogramming ● End-to-end integrated stack ● Enables you to focus on the business problem
  4. 4. Best-of-Breed Java Technologies ● Spring ● Spring MVC ● Hibernate ● log4j ● jUnit ● SiteMesh
  5. 5. Integrated Stack ● Layers and technologies integrated and wired out-of-the-box ● Default configurations based on industry best practices ● 80/20 rule
  6. 6. Directory Structure ● grails-app - top level directory for Groovy sources ● conf - Configuration sources. ● controller - Web controllers - The C in MVC. ● domain - The application domain. ● i18n - Support for internationalization (i18n). ● services - The service layer. ● taglib - Tag libraries. ● views - Groovy Server Pages. ● scripts - Gant/Gradle scripts. ● src - Supporting sources ● groovy - Other Groovy sources ● java - Other Java sources ● test - Unit and integration test
  7. 7. GORM ● Object Relational Mapping (ORM) ● Data access layer ● Simplifies configuration through conventions ● Defaults to Hibernate implementation ● Provided as plugin, alternatives pluggable ● Extends and simplifies data access APIs ● Similar to ActiveRecord in Rails
  8. 8. GORM Domain Modeling //this is a complete Hibernate mapping! class Employee { String firstName String lastName Date startDate }
  9. 9. GORM Dynamic CRUD def employee = Employee.get(1) employee.delete() def newEmployee = new Employee(firstName: “Joe”, lastName: ”Programmer”) newEmployee.save()
  10. 10. GORM Dynamic finders Employee.findByLastNameAndHireDateGreaterThan(“Jones”, someDate) findBy and findAllBy ● InList - In the list of given values ● LessThan - less than the given value ● LessThanEquals - less than or equal a give value ● GreaterThan - greater than a given value ● GreaterThanEquals - greater than or equal a given value ● Like - Equivalent to a SQL like expression ● Ilike - Similar to a Like, except case insensitive ● NotEqual - Negates equality ● Between - Between two values (requires two arguments) ● IsNotNull - Not a null value (doesn't require an argument) ● IsNull - Is a null value (doesn't require an argument)
  11. 11. GORM Hibernate HQL Employee.findAll("from Employee as e where e.lastName like :lastName", [lastName:"Jon%"]) //with pagination and sorting Employee.findAll("from Employee as e where e.lastName like :lastName", [lastName:"Jon%", max:10, offset:20, sort:"hireDate", order:"asc"])
  12. 12. GORM Simplified Hibernate Criteria API // Find incomplete tasks assigned to Jones where the company is Monsanto // and the project name begins with “Rubic”, order by task name def criteria = Tasks.createCriteria() def tasks = criteria.list { eq(‘completed’, false) project{ like(‘name’ ‘Rubic%’) company{ eq(‘name’, ‘Monsanto’) } assignedEmployees{ eq(‘lastName’, ‘Jones’) } } order(‘taskName’, ‘asc’) }
  13. 13. Web Layer ● Controllers built on Spring MVC ● URL mapping conventions map requests to controllers ● Naming and directory conventions map actions to views ● Built-in AOP action interceptors ● Every controller provides a beforeInterceptor and afterInterceptor ● Specifiable by action, optionally with patterns and exclusions ● Servlet objects and convenience extensions injected into controller actions at runtime and provided as implicit variables ● servletContext, session, request, response, params, flash
  14. 14. Spring MVC Request parameters parsed into multidimensional params map ● Easily accessed with powerful Groovy map support. <!-- HTML form --> <input type=”text” name=”userName” /> //controller code def userName = params.userName <!-- HTML form with dot-notation for complex embedded objects --> <input type=”text” name=”user.address.zipCode” /> <input type=”text” name=”user.address.state” /> //controller code def zipCode = params.user.address.zipCode def state = params.user.address.state
  15. 15. Data Binding def save = { //bind params to new instance def user = new User(params) //persist user.save() } def update = { //get instance from database def user = User.get(params.id) //bind params user.properties = params user.save() }
  16. 16. Request Format Transparency Grails codecs (dynamic encode/decode methods) easily automap formats like XML and JSON to the params map. <!-- XML request --> <user> <id>42</id> <address> <zipCode>63122</zipCode> </address> </user> //transparent to the controller code def zipCode = params.user.address.zipCode def user = new User(params.user) Easily create custom codecs to support specific requirements
  17. 17. XML & JSON Marshalling Groovy's popular XML support + Grails builders and codecs + Convenience methods def list = { render Project.list() as XML } def list = { render Project.list() as JSON }
  18. 18. Groovy Server Pages (GSP) ● Similar to JSP ● Tag library like JSTL, but much more powerful ● Easy custom tags ● Powerful templates ● SiteMesh is automatically configured for layout management
  19. 19. Groovy Server Pages (GSP) Tags actionSubmit fieldValue layoutBody render applyLayout findAll layoutHead renderErrors checkBox form layoutTitle resource collect formRemote link select cookie formatBoolean localeSelect set country formatDate message sortableColumn countrySelect formatNumber meta submitButton createLink grep pageProperty submitToRemote createLinkTo hasErrors paginate textArea currencySelect header passwordField textField datePicker hiddenField radio timeZoneSelect each if radioGroup unless eachError include remoteField uploadForm else javascript remoteFunction while elseif join remoteLink
  20. 20. AJAX ● Bundled with Prototype and Script.aculo.us ● Excellent jQuery plugin ● GSP tags for AJAX ● Adapters built-in for major JavaScript frameworks ● Plugins provide additional AJAX functionality
  21. 21. Configuration Per-Environment Configuration Supports SCM ● Package or run as any configured environment. Default environments and build configurations defined for development, test (staging) and production. ● Programmatic environment detection with provided Environment class. environments { development { dataSource { dbCreate = "create-drop" // one of 'create', 'create-drop','update' url = "jdbc:hsqldb:mem:devDB" } } test { dataSource { dbCreate = "update" url = "jdbc:hsqldb:mem:testDb" } } production { dataSource { dbCreate = "update" url = "jdbc:hsqldb:file:prodDb;shutdown=true" } } }
  22. 22. Plugins Grails extensions are provided as plugins ● Created by core Grails team and community ● Powerful – a plugin IS a Grails application ● Plugins typically bring the idioms of convention-over-configuration and simplified APIs to popular libraries ● Easy to create your own – provides a strong architecture for re-use across projects ● Examples ● Spring Security ● JMS ● Spring WS ● LDAP ● Quartz
  23. 23. Plugins Plugin development is very active
  24. 24. The Bottom Line ● Increases developer productivity ● less code + less configuration = easier to maintain ● Smaller learning curve than other Java web frameworks ● Eases the learning curve for the underlying technologies ● Project layout conventions and a standard technology stack promote quicker ramp-up time from one Grails project to another ● Increases agility potential through rapid proto-typing and quick customer feedback ● Guides solid technology choices by providing excellent defaults ● Developers enjoy it, which promotes morale and retention
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×