• Save
Os Davis
Upcoming SlideShare
Loading in...5
×
 

Os Davis

on

  • 1,179 views

 

Statistics

Views

Total Views
1,179
Views on SlideShare
1,177
Embed Views
2

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 2

http://static.slidesharecdn.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Os Davis Os Davis Presentation Transcript

  • 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 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
  • 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
  • What is Grails? Grails is a fully integrated modern Java web application in a box: © 2007, Davisworld.org 4
  • 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. © 2007, Davisworld.org 8
  • Act 1: For Those in a Hurry… 9
  • 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
  • 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
  • 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 $ grails -Dserver.port=9090 run-app Option #2: edit GRAILS_HOME/scripts/Init.groovy (see next page…) © 2007, Davisworld.org 16
  • © 2007, Davisworld.org 17
  • 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
  • Changing the Database © 2007, Davisworld.org 19
  • 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
  • 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
  • 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
  • Point Grails to MySQL © 2007, Davisworld.org 23
  • 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
  • Bootstrapping Data © 2007, Davisworld.org 25
  • Bootstrapping Gotcha If you flipped dbCreate to “update”, beware: ApplicationBootStrap gets run each time © 2007, Davisworld.org 26
  • 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
  • Changing the Home Page The default homepage is web-app/index.gsp. You can redirect to any page or controller: © 2007, Davisworld.org 28
  • 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 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
  • 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
  • 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
  • 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://localhost:9090/bookstore/book/list Corresponding list.gsp BookController.show(5) http://localhost:9090/bookstore/book/show/5 © 2007, Davisworld.org 36
  • 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 the next line, the POGO is saved to the database via GORM. © 2007, Davisworld.org 40
  • 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()
  • 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
  • Act 4: Understanding Grails Models… …and Views… …and GORM… (they’re all interrelated) 43
  • 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
  • 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 | +------------------+--------------+ | 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
  • 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 53
  • Conclusion Thanks for your time! Questions? Email: scottdavis99@yahoo.com Download slides: http://www.davisworld.org/presentations © 2007, Davisworld.org 54