Your SlideShare is downloading. ×
Real World Grails

From download to production
              and beyond…




                          1
Introduction
   My name is Scott Davis
       Editor in Chief of http://aboutGroovy.com
       Author
          Groovy Rec...
Have you ever noticed that you use
                            Spring to save time,
                         Hibernate to ...
What is Grails?

   Grails is a fully integrated modern
   Java web application in a box:




© 2007, Davisworld.org      ...
Included JARs




© 2007, Davisworld.org   5
Included Ajax Support




© 2007, Davisworld.org   6
Included Ajax Support




© 2007, Davisworld.org   7
(Almost) Included Ajax
Support

$ grails install-dojo
   -- Installs the Dojo toolkit.
   An advanced Javascript library.
...
Act 1:
For Those in a Hurry…



                         9
Installing Grails
                   http://grails.org



!      Download/unzip grails-bin.tar.gz
       (or zip)
!      C...
Your 1-Slide Guide to Grails
Type the following:
    $ grails create-app bookstore
    $ cd bookstore
    $ grails create-...
Controller
    Model




     View




  © 2007, Davisworld.org   12
Generated List




© 2007, Davisworld.org   13
Generated Show




© 2007, Davisworld.org   14
Act 2:
Tweaking the defaults…



                          15
Changing the Port
   Grails / Jetty runs on port 8080 by
   default

       Option #1: change the port at runtime

  $ gra...
© 2007, Davisworld.org   17
Changing Grails
Environments

   Dev (the default) auto-reloads
   changes to Controllers, Views, and
   even the Model
  ...
Changing the Database




© 2007, Davisworld.org   19
Why does my data go
away?
   dbCreate == hibernate.hbm2ddl.auto
       Create-drop -- creates the tables on startup,
     ...
Changing to MySQL

1) Create the database and user
2) Copy the driver into lib
3) Adjust values in
    grails-app/conf/Dev...
Create the database
$ mysql --user=root
Welcome to the MySQL monitor.

mysql> create database bookstore_dev;

mysql> grant...
Point Grails to MySQL




© 2007, Davisworld.org   23
mysql> show tables;
 +-------------------------+
Magic Occurs
 | Tables_in_bookstore_dev |
 +-------------------------+
 |...
Bootstrapping Data




© 2007, Davisworld.org   25
Bootstrapping Gotcha
   If you flipped dbCreate to “update”,
   beware:
       ApplicationBootStrap gets run each time



...
Changing the Web server

   To run your app in Tomcat instead
   of Jetty:
$ grails war
$ cp bookstore.war /opt/tomcat/web...
Changing the Home Page
The default homepage is web-app/index.gsp.
You can redirect to any page or controller:




© 2007, ...
Act 3:
Understanding Grails
       Controllers…



                       29
Generating a Controller
$ grails generate-controller




© 2007, Davisworld.org         30
The Three R’s
         Each controller method
        ends in one of three ways:

   Redirect
       Equivalent to respons...
Controller.index


Index is the default target,
                                         Params is a Map
just like index.j...
Controller.list



   Implicit return
                         GORM
   statement
                         (Grails Object/R...
List.gsp
                         Returned from Controller




© 2007, Davisworld.org                              34
List view




© 2007, Davisworld.org   35
Convention over
Configuration
BookController
    http://localhost:9090/bookstore/book
BookController.list
    http://local...
Show view




© 2007, Davisworld.org   37
Controller.show




© 2007, Davisworld.org   38
Create.gsp               Controller Method




© 2007, Davisworld.org                       39
Controller.save




In one line, Param name/value pairs from the form
are saved to a POGO (Plain Old Groovy Object).

In t...
Super-sneaky Grails Shell
$ grails shell
Let's get Groovy!
================
groovy> Book.list()
groovy> go
===> [Book : 5,...
Auto-scaffolding
  I like generating Controllers and Views if I know
  that I am going to be tweaking them by hand.

  Oth...
Act 4:
   Understanding Grails
                 Models…
          …and Views…
          …and GORM…
(they’re all interrelat...
POGOs
   Plain Old Groovy Objects
       Fields are automatically
       private
       Getters and setters are
       aut...
Specifying Field Order




© 2007, Davisworld.org   45
Ordered Fields in List




© 2007, Davisworld.org   46
Field Validation




© 2007, Davisworld.org   47
Create Form




© 2007, Davisworld.org   48
Validation




© 2007, Davisworld.org   49
mysql> desc book;
Schema
+------------------+--------------+
| Field            | Type         |
+------------------+-----...
GORM:
One-to-many




© 2007, Davisworld.org   51
One-to-Many




© 2007, Davisworld.org   52
Conclusion

   Grails is a fully integrated modern
   Java web application in a box:




© 2007, Davisworld.org           ...
Conclusion
    Thanks for your time!
        Questions?

        Email:
           scottdavis99@yahoo.com
        Download...
Upcoming SlideShare
Loading in...5
×

Os Davis

556

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
556
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Os Davis"

  1. 1. Real World Grails From download to production and beyond… 1
  2. 2. Introduction My name is Scott Davis Editor in Chief of http://aboutGroovy.com Author Groovy Recipes: Greasing the Wheels of Java (Pragmatic Bookshelf) GIS for Web Developers (Pragmatic Bookshelf) Google Maps API (Pragmatic Bookshelf) JBoss At Work (O’Reilly) © 2007, Davisworld.org 2
  3. 3. Have you ever noticed that you use Spring to save time, Hibernate to save time, Ant to save time… Does it feel like you are saving any time at all? Why doesn’t the whole come close to feeling like the sum of its parts? © 2007, Davisworld.org 3
  4. 4. What is Grails? Grails is a fully integrated modern Java web application in a box: © 2007, Davisworld.org 4
  5. 5. Included JARs © 2007, Davisworld.org 5
  6. 6. Included Ajax Support © 2007, Davisworld.org 6
  7. 7. Included Ajax Support © 2007, Davisworld.org 7
  8. 8. (Almost) Included Ajax Support $ grails install-dojo -- Installs the Dojo toolkit. An advanced Javascript library. © 2007, Davisworld.org 8
  9. 9. Act 1: For Those in a Hurry… 9
  10. 10. Installing Grails http://grails.org ! Download/unzip grails-bin.tar.gz (or zip) ! Create GRAILS_HOME ! Add $GRAILS_HOME/bin to PATH © 2007, Davisworld.org 10
  11. 11. Your 1-Slide Guide to Grails Type the following: $ grails create-app bookstore $ cd bookstore $ grails create-domain-class book (Add fields to grails-app/domain/Book.groovy) $ grails generate-all book $ grails run-app $ grails help -- shows all available commands © 2007, Davisworld.org 11
  12. 12. Controller Model View © 2007, Davisworld.org 12
  13. 13. Generated List © 2007, Davisworld.org 13
  14. 14. Generated Show © 2007, Davisworld.org 14
  15. 15. Act 2: Tweaking the defaults… 15
  16. 16. Changing the Port Grails / Jetty runs on port 8080 by default Option #1: change the port at runtime $ grails -Dserver.port=9090 run-app Option #2: edit GRAILS_HOME/scripts/Init.groovy (see next page…) © 2007, Davisworld.org 16
  17. 17. © 2007, Davisworld.org 17
  18. 18. Changing Grails Environments Dev (the default) auto-reloads changes to Controllers, Views, and even the Model This is helpful for rapid development Prod loads all items statically for maximum performance © 2007, Davisworld.org 18
  19. 19. Changing the Database © 2007, Davisworld.org 19
  20. 20. Why does my data go away? dbCreate == hibernate.hbm2ddl.auto Create-drop -- creates the tables on startup, drops them on shutdown (DEV) Create -- creates the tables on startup, just deletes the data on shutdown Update -- creates the tables on startup, saves the data between restarts (PROD, TEST) Remove the value to manage the schema manually © 2007, Davisworld.org 20
  21. 21. Changing to MySQL 1) Create the database and user 2) Copy the driver into lib 3) Adjust values in grails-app/conf/DevelopmentDataSource.groovy © 2007, Davisworld.org 21
  22. 22. Create the database $ mysql --user=root Welcome to the MySQL monitor. mysql> create database bookstore_dev; mysql> grant all on bookstore_dev.* to grails@localhost identified by 'server'; mysql> flush privileges; Sanity check the newly created login: $ mysql --user=grails -p --database=bookstore_dev © 2007, Davisworld.org 22
  23. 23. Point Grails to MySQL © 2007, Davisworld.org 23
  24. 24. mysql> show tables; +-------------------------+ Magic Occurs | Tables_in_bookstore_dev | +-------------------------+ | book | +-------------------------+ mysql> desc book; +---------+--------------+------+-----+ | Field | Type | Null | Key | +---------+--------------+------+-----+ | id | bigint(20) | NO | PRI | | version | bigint(20) | NO | | | title | varchar(255) | NO | | | author | varchar(255) | NO | | +---------+--------------+------+-----+ © 2007, Davisworld.org 24
  25. 25. Bootstrapping Data © 2007, Davisworld.org 25
  26. 26. Bootstrapping Gotcha If you flipped dbCreate to “update”, beware: ApplicationBootStrap gets run each time © 2007, Davisworld.org 26
  27. 27. Changing the Web server To run your app in Tomcat instead of Jetty: $ grails war $ cp bookstore.war /opt/tomcat/webapps/ Gotcha: Grails WARs run in PROD by default. $ grails dev war Or run your container with JAVA_OPTS=-Dgrails.env=development © 2007, Davisworld.org 27
  28. 28. Changing the Home Page The default homepage is web-app/index.gsp. You can redirect to any page or controller: © 2007, Davisworld.org 28
  29. 29. Act 3: Understanding Grails Controllers… 29
  30. 30. Generating a Controller $ grails generate-controller © 2007, Davisworld.org 30
  31. 31. The Three R’s Each controller method ends in one of three ways: Redirect Equivalent to response.sendRedirect() redirect(action:list,params:params) Return Calls a GSP named the same as the method return [ bookList: Book.list( params ) ] Render Calls a GSP of an arbitrary name render(view:'edit',model:[book:book]) © 2007, Davisworld.org 31
  32. 32. Controller.index Index is the default target, Params is a Map just like index.jsp or index.html of the QueryString name/value pairs redirect() == response.sendRedirect() action:list == the list method in this controller © 2007, Davisworld.org 32
  33. 33. Controller.list Implicit return GORM statement (Grails Object/Relational Mapping) Map of named objects in the Response (see list.gsp, next page) © 2007, Davisworld.org 33
  34. 34. List.gsp Returned from Controller © 2007, Davisworld.org 34
  35. 35. List view © 2007, Davisworld.org 35
  36. 36. Convention over Configuration BookController http://localhost:9090/bookstore/book BookController.list http://localhost:9090/bookstore/book/list Corresponding list.gsp BookController.show(5) http://localhost:9090/bookstore/book/show/5 © 2007, Davisworld.org 36
  37. 37. Show view © 2007, Davisworld.org 37
  38. 38. Controller.show © 2007, Davisworld.org 38
  39. 39. Create.gsp Controller Method © 2007, Davisworld.org 39
  40. 40. Controller.save In one line, Param name/value pairs from the form are saved to a POGO (Plain Old Groovy Object). In the next line, the POGO is saved to the database via GORM. © 2007, Davisworld.org 40
  41. 41. Super-sneaky Grails Shell $ grails shell Let's get Groovy! ================ groovy> Book.list() groovy> go ===> [Book : 5, Book : 6, Book : 7, Book : 8, Book : 9, Book : 10] groovy> b = Book.get(5) groovy> b.title groovy> go ===> Groovy Recipes © 2007, Davisworld.org 41 groovy> new Book(author:’foo’).save()
  42. 42. Auto-scaffolding I like generating Controllers and Views if I know that I am going to be tweaking them by hand. Otherwise, allowing Grails to auto-scaffold my Controllers and Views in memory reduces by code footprint to… (I don’t even bother with the Grails CLI for these…) © 2007, Davisworld.org 42
  43. 43. Act 4: Understanding Grails Models… …and Views… …and GORM… (they’re all interrelated) 43
  44. 44. POGOs Plain Old Groovy Objects Fields are automatically private Getters and setters are automatically provided Use Wrappers instead of Primitives Integer, Float, Double, Boolean © 2007, Davisworld.org 44
  45. 45. Specifying Field Order © 2007, Davisworld.org 45
  46. 46. Ordered Fields in List © 2007, Davisworld.org 46
  47. 47. Field Validation © 2007, Davisworld.org 47
  48. 48. Create Form © 2007, Davisworld.org 48
  49. 49. Validation © 2007, Davisworld.org 49
  50. 50. mysql> desc book; Schema +------------------+--------------+ | Field | Type | +------------------+--------------+ | id | bigint(20) | | version | bigint(20) | | title | varchar(50) | | pages | int(11) | | category | varchar(255) | | isbn | varchar(255) | | excerpt | text | | publication_date | datetime | | cover | varchar(255) | | author | varchar(255) | +------------------+--------------+ © 2007, Davisworld.org 50
  51. 51. GORM: One-to-many © 2007, Davisworld.org 51
  52. 52. One-to-Many © 2007, Davisworld.org 52
  53. 53. Conclusion Grails is a fully integrated modern Java web application in a box: © 2007, Davisworld.org 53
  54. 54. Conclusion Thanks for your time! Questions? Email: scottdavis99@yahoo.com Download slides: http://www.davisworld.org/presentations © 2007, Davisworld.org 54

×