0
Agile Web Development withGroovy and Grails Carol McDonald, Java Architect
ObjectiveOverview of the    Grails Web Platform                  2
Groovy Overview                  3
What is Groovy?• A dynamic language written for the JVM  > Generates byte code• Java like syntax  > Easy learning curve fo...
A Valid Java Programimport java.util.*;public class Erase {    private List<String> filterLongerThan(List<String> strings,...
A Valid Groovy Programimport java.util.*;public class Erase {    private List<String> filterLongerThan(List<String> string...
The Groovy Waydef names = ["Ted", "Fred", "Jed", "Ned"]println namesdef shortNames = names.findAll{    it.size() <= 3}prin...
Grails Overview                  8
What is Grails?• An Open Source Groovy MVC framework for  web applications• Principles  > CoC – Convention over configurat...
Why Grails?• ORM layer can be overly difficult to master and get right  > A return to POJO and annotations in JPA but stil...
Grails Technology Stack                          11
How To Get Started• Download Grails  > http://grails.org• Configure Netbeans 6.5• Download Groovy  > http://groovy.codehau...
Using Grails               13
grails create­app• Will be prompted for the name of the application                                       IDE runs the    ...
Grails DirectoryStructureApplication  name                   Grail sourceAdditional  JARs    Web  application             ...
Netbeans GrailsProject StructureProject name                    Grail sourceAdditional  JARs    Web  application          ...
Configure for MySQL• copy the mysql-connector-java-5.1.6-bin.jar to the lib directory• Edit DataSource.groovydataSource { ...
MVC and Grails                 • Models, or domain                  classes, represent the                  problem domain...
grails create­domain­class• The Model is your applications persistent business domain objects.                            ...
add the domain class attributesclass Item {  Long id  String name  String description  String imageurl  String imagethumbu...
Persistence Methods• GORM automatically provides persistence  methods to your object  > save(), get(), delete()• Automatic...
Queries• Dynamically generated queries  > list, findBy• Manual queries (not discussed here)  > HibernateQL• Examples  > It...
grails domain class  Item                                 Address String name          1           M   String street Strin...
Scaffolding• Generates CRUD actions and views for the  corresponding domain class• Dynamic:  > Enable the scaffold propert...
Scaffolding• Generates a controller and views for the domain class                                                    25
MVC and Grails                 • Controllers control                  request flow, interact                  with models,...
grails create­controller  • Controller made up of action methods    > Grails routes requests to the action corresponding t...
URL Mappings• Default mapping from URL to action method         http://host/catalog/item/list              Application   C...
grails controllerclass ItemController {       def index = { redirect(action:list,params:params) }   def list = {      if(!...
MVC and Grails   • Views are defined in Groovy     Server Pages (GSP) and     render the model                            ...
grails view • actions usually render a Groovy Server Page in the   views directory corresponding to the name of the   cont...
grails run­app• Will start the embedded web server and run the  application• Defaults to  > http://localhost:8080/<app_nam...
Summary• Groovy is a powerful dynamic language for the  JVM• Grails is a full featured web platform                       ...
http://weblogs.java.net/blog/caroljmcdonald /                                              34
Acknowldegement• Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides                  ...
●    Carol McDonald    Java Architect     Tech Days 2009                      36
Lots of Books and Online Tutorials                                     37
Upcoming SlideShare
Loading in...5
×

Groovygrailsnetbeans 12517452668498-phpapp03

351

Published on

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

  • Be the first to like this

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

No notes for slide

Transcript of "Groovygrailsnetbeans 12517452668498-phpapp03"

  1. 1. Agile Web Development withGroovy and Grails Carol McDonald, Java Architect
  2. 2. ObjectiveOverview of the Grails Web Platform 2
  3. 3. Groovy Overview 3
  4. 4. What is Groovy?• A dynamic language written for the JVM > Generates byte code• Java like syntax > Easy learning curve for Java developers• Seamless integration with Java > A Groovy object is a Java Object > Groovy application can create Java objects > Java objects can create Groovy objects 4
  5. 5. A Valid Java Programimport java.util.*;public class Erase { private List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String n: strings) if (n.length() <= length) result.add(n); return (result); } public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); }} 5
  6. 6. A Valid Groovy Programimport java.util.*;public class Erase { private List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String n: strings) if (n.length() <= length) result.add(n); return (result); } public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); }} 6
  7. 7. The Groovy Waydef names = ["Ted", "Fred", "Jed", "Ned"]println namesdef shortNames = names.findAll{ it.size() <= 3}print shortNames 7
  8. 8. Grails Overview 8
  9. 9. What is Grails?• An Open Source Groovy MVC framework for web applications• Principles > CoC – Convention over configuration > DRY – Dont repeat yourself• Similar to RoR but with tighter integration to the Java platform 9
  10. 10. Why Grails?• ORM layer can be overly difficult to master and get right > A return to POJO and annotations in JPA but still lots of stuff to learn• Numerous layers and configuration files lead to chaos > Adoption of frameworks a good thing, but too often lead to configuration file hell• Ugly JSPs with scriptlets and complexity of JSP custom tags• Grails addresses these without compromising their benefits 10
  11. 11. Grails Technology Stack 11
  12. 12. How To Get Started• Download Grails > http://grails.org• Configure Netbeans 6.5• Download Groovy > http://groovy.codehaus.org• Set GROOVY_HOME, GRAILS_HOME For command• Add bin to your PATH line > $GROOVY_HOME/bin:$GRAILS_HOME/bin 12
  13. 13. Using Grails 13
  14. 14. grails create­app• Will be prompted for the name of the application IDE runs the "grails create-app" command• Generate a directory structure for > Grails source > Additional libraries > Configurations > web-app 14
  15. 15. Grails DirectoryStructureApplication name Grail sourceAdditional JARs Web application 15
  16. 16. Netbeans GrailsProject StructureProject name Grail sourceAdditional JARs Web application 16
  17. 17. Configure for MySQL• copy the mysql-connector-java-5.1.6-bin.jar to the lib directory• Edit DataSource.groovydataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" username = "root" password = "" dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"}environments { development { dataSource { dbCreate = "update" // one of create, create-drop,update url = "jdbc:mysql://localhost/petcatalog" } } 17
  18. 18. MVC and Grails • Models, or domain classes, represent the problem domain 18
  19. 19. grails create­domain­class• The Model is your applications persistent business domain objects. 19
  20. 20. add the domain class attributesclass Item { Long id String name String description String imageurl String imagethumburl BigDecimal price} Groovy with Grails dynamically generates getters and setters and the dynamic methods Item.save(), Item.delete(), Item.list(), Item.get() to retrieve/update data from/to the db table. 20
  21. 21. Persistence Methods• GORM automatically provides persistence methods to your object > save(), get(), delete()• Automatically generates addTo*() for object relationships > Where * is the name of the propertydef item = new Item(name:Fred)def order =  new Order(orderDate: new Date(), item: PSP)item.addToOrders(order)item.save() 21
  22. 22. Queries• Dynamically generated queries > list, findBy• Manual queries (not discussed here) > HibernateQL• Examples > Item.list() > Item.findByName(nice cat) 22
  23. 23. grails domain class Item Address String name 1 M String street String description String zip Address address static hasMany = [item:Item]• Define relation between objects with attributes > hasMany, belongsTo > static hasMany = [ item: Item ] Groovy hashmap 23
  24. 24. Scaffolding• Generates CRUD actions and views for the corresponding domain class• Dynamic: > Enable the scaffold property in the controller class def scaffold = true• Static > grails generate­all > Generates a controller and views for a  domain class• Useful to get up and running quickly 24
  25. 25. Scaffolding• Generates a controller and views for the domain class 25
  26. 26. MVC and Grails • Controllers control request flow, interact with models, and delegate to views. 26
  27. 27. grails create­controller • Controller made up of action methods > Grails routes requests to the action corresponding to URL mapping > Default action is index class ItemController {     def index = { redirect(action:list,params:params) } def list = { http://host/catalog/item/list if(!params.max) params.max = 10 [ itemList: Item.list( params ) ] } def show = {...Actions 27
  28. 28. URL Mappings• Default mapping from URL to action method http://host/catalog/item/list Application Controller Action• Defined in > grails­app/conf/UrlMappings.groovy static mappings = {       "/$controller/$action?/$id?" 28
  29. 29. grails controllerclass ItemController {     def index = { redirect(action:list,params:params) } def list = { if(!params.max) params.max = 10 [ itemInstanceList: Item.list( params ) ] } def show = {... returns an ArrayList of item objects retrieved from the item database table itemInstanceList variable is made available to the view 29
  30. 30. MVC and Grails • Views are defined in Groovy Server Pages (GSP) and render the model 30
  31. 31. grails view • actions usually render a Groovy Server Page in the views directory corresponding to the name of the controller and action. list.gsp<html> <table> g:each Groovy Tag loops through each object in the  <tbody> itemInstanceList variable      <g:each in="${itemInstanceList}" status="i" var="itemInstance">                        <td>${fieldValue(bean:itemInstance, field:price)}</td>          </tr> displays the value of the item s price attribute      </g:each>  </tbody> </table> 31
  32. 32. grails run­app• Will start the embedded web server and run the application• Defaults to > http://localhost:8080/<app_name> 32
  33. 33. Summary• Groovy is a powerful dynamic language for the JVM• Grails is a full featured web platform 33
  34. 34. http://weblogs.java.net/blog/caroljmcdonald / 34
  35. 35. Acknowldegement• Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides 35
  36. 36. ● Carol McDonald Java Architect Tech Days 2009 36
  37. 37. Lots of Books and Online Tutorials 37
  1. A particular slide catching your eye?

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

×