Agile Web Development   with Groovy and Grails Carol McDonald, Java Architect
Objective Overview of the Grails  Web Platform
Groovy Overview
What is Groovy? <ul><li>A dynamic language written for the JVM </li></ul><ul><ul><li>Generates byte code </li></ul></ul><u...
A Valid Java Program <ul><li>import java.util.*; </li></ul><ul><li>public class Erase { </li></ul><ul><ul><li>private List...
A Valid Groovy Program <ul><li>import java.util.*; </li></ul><ul><li>public class Erase { </li></ul><ul><ul><li>private Li...
The Groovy Way <ul><ul><li>def names = [&quot;Ted&quot;, &quot;Fred&quot;, &quot;Jed&quot;, &quot;Ned&quot;] </li></ul></u...
Grails Overview
What is Grails? <ul><li>An Open Source Groovy MVC framework for web applications </li></ul><ul><li>Principles </li></ul><u...
Why Grails? <ul><li>ORM layer can be overly difficult to master and get right </li></ul><ul><ul><li>A return to POJO and a...
Grails Technology Stack
How To Get Started <ul><li>Download Grails </li></ul><ul><ul><li>http://grails.org </li></ul></ul><ul><li>Configure Netbea...
Using Grails
grails create-app <ul><li>Will be prompted for the name of the application </li></ul><ul><li>Generate a directory structur...
Grails Directory  Structure Application name Additional JARs Web application Grail source
Netbeans Grails  Project Structure Project  name Grail source Additional JARs Web application
Configure for MySQL <ul><li>copy the  mysql-connector-java-5.1.6-bin.jar to the lib directory </li></ul><ul><li>Edit DataS...
MVC and Grails <ul><li>Models, or domain classes, represent the problem domain </li></ul>
grails create-domain-class <ul><li>The Model is your application's persistent business domain objects. </li></ul>
add the domain class attributes <ul><ul><li>class Item { </li></ul></ul><ul><ul><li>Long id </li></ul></ul><ul><ul><li>Str...
Persistence Methods <ul><li>GORM automatically provides persistence methods to your object </li></ul><ul><ul><li>save() , ...
Queries <ul><li>Dynamically generated queries </li></ul><ul><ul><li>list ,  findBy </li></ul></ul><ul><li>Manual queries (...
grails domain class <ul><li>Define relation between objects with attributes </li></ul><ul><ul><li>hasMany ,  belongsTo </l...
Scaffolding <ul><li>Generates CRUD actions and views for the corresponding domain class </li></ul><ul><li>Dynamic: </li></...
Scaffolding <ul><li>Generates a controller and views for the domain class </li></ul>Demo
MVC and Grails <ul><li>Controllers control request flow, interact with models, and delegate to views. </li></ul>
grails create-controller <ul><li>Controller made up of  action  methods </li></ul><ul><ul><li>Grails routes requests to th...
URL Mappings <ul><li>Default mapping from URL to action method </li></ul><ul><ul><ul><li>http://host/catalog/item/list </l...
grails controller class  Item Controller   {  def  index  = { redirect(action:list,params:params) } def  list  = { if(!par...
MVC and Grails <ul><li>Views are defined in Groovy Server Pages (GSP) and render the model </li></ul>
grails view <ul><li>actions usually render a  Groovy Server Page  in the views directory corresponding to the name of the ...
grails run-app <ul><li>Will start the embedded web server and run the application </li></ul><ul><li>Defaults to </li></ul>...
Summary <ul><li>Groovy is a powerful dynamic language for the JVM </li></ul><ul><li>Grails is a full featured web platform...
http://weblogs.java.net/blog/caroljmcdonald/
Acknowldegement <ul><li>Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides </li></ul>
<ul><li>Presenter’s Name </li></ul><ul><ul><li>[email_address] </li></ul></ul>Carol McDonald Java Architect Tech Days 2009
Lots of Books and Online Tutorials
Upcoming SlideShare
Loading in...5
×

Agile web development Groovy Grails with Netbeans

4,597

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,597
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
58
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Agile web development Groovy Grails with Netbeans

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

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

×