SlideShare a Scribd company logo
Agile Web Development   with Groovy and Grails Carol McDonald, Java Architect
Objective Overview of the Grails  Web Platform
Groovy Overview
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
A Valid Java Program import 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(&quot;Ted&quot;); names.add(&quot;Fred&quot;); names.add(&quot;Jed&quot;); names.add(&quot;Ned&quot;); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); } }
A Valid Groovy Program import 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(&quot;Ted&quot;); names.add(&quot;Fred&quot;); names.add(&quot;Jed&quot;); names.add(&quot;Ned&quot;); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); } }
The Groovy Way def names = [&quot;Ted&quot;, &quot;Fred&quot;, &quot;Jed&quot;, &quot;Ned&quot;] println names  def shortNames = names.findAll{  it.size() <= 3  } print shortNames
Grails Overview
What is Grails? An Open Source Groovy MVC framework for web applications Principles CoC – Convention over configuration DRY – Don't repeat yourself Similar to RoR but with tighter integration to the Java platform
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
Grails Technology Stack
How To Get Started Download Grails http://grails.org Configure Netbeans 6.5 Download Groovy http://groovy.codehaus.org Set  GROOVY_HOME ,  GRAILS_HOME Add bin to your PATH $GROOVY_HOME/bin:$GRAILS_HOME/bin For command line
Using Grails
grails create-app Will be prompted for the name of the application Generate a directory structure for  Grails source Additional libraries Configurations web-app IDE runs the  &quot;grails create-app&quot;  command Demo
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 copy the  mysql-connector-java-5.1.6-bin.jar to the lib directory Edit DataSource.groovy dataSource { pooled = true driverClassName = &quot;com.mysql.jdbc.Driver&quot; username = &quot;root&quot; password = &quot;&quot;  dialect = &quot;org.hibernate.dialect.MySQL5InnoDBDialect&quot; } environments { development { dataSource { dbCreate = &quot;update&quot;  // one of 'create', 'create-drop','update' url = &quot;jdbc:mysql://localhost/petcatalog&quot; } }
MVC and Grails Models, or domain classes, represent the problem domain
grails create-domain-class The Model is your application's persistent business domain objects.
add the domain class attributes class 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.
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 property def item = new Item(name:'Fred') def order =  new Order(orderDate: new Date(), item: 'PSP') item. addToOrders (order) item. save ()
Queries Dynamically generated queries list ,  findBy Manual queries (not discussed here) HibernateQL Examples Item.list() Item.findBy Name ('nice cat')
grails domain class Define relation between objects with attributes hasMany ,  belongsTo static hasMany = [ item: Item ] Groovy hashmap Address String street String zip static hasMany = [item:Item] 1 M Item String name String description Address address
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
Scaffolding Generates a controller and views for the domain class Demo
MVC and Grails Controllers control request flow, interact with models, and delegate to views.
grails create-controller Controller made up of  action  methods Grails routes requests to the  action  corresponding to  URL  mapping  Default action is  index 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
URL Mappings Default mapping from URL to action method http://host/catalog/item/list Defined in  grails-app/conf/UrlMappings.groovy static mappings = { &quot;/$controller/$action ? /$id ? &quot; Application Controller Action
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
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 controller and action.  list.gsp <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
grails run-app Will start the embedded web server and run the application Defaults to http://localhost:8080/<app_name> Demo
Summary Groovy is a powerful dynamic language for the JVM Grails is a full featured web platform
http://weblogs.java.net/blog/caroljmcdonald/
Acknowldegement Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides
Presenter’s Name [email_address] Carol McDonald Java Architect Tech Days 2009
Lots of Books and Online Tutorials

More Related Content

PPTX
Introduction to Grails Framework
PPT
Scripting Oracle Develop 2007
PDF
Integrating React.js Into a PHP Application: Dutch PHP 2019
PDF
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
PDF
Introduction to GraphQL
PDF
JavaOne 2013: Java 8 - The Good Parts
PDF
Managing GraphQL servers with AWS Fargate & Prisma Cloud
Introduction to Grails Framework
Scripting Oracle Develop 2007
Integrating React.js Into a PHP Application: Dutch PHP 2019
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Introduction to GraphQL
JavaOne 2013: Java 8 - The Good Parts
Managing GraphQL servers with AWS Fargate & Prisma Cloud

What's hot (20)

ODP
Object Oriented Javascript
PPSX
JS Fest 2018. Сергей Пузанков. E2E-тестирование фронтенда c Hermione
PDF
Restful App Engine
PDF
Create responsive websites with Django, REST and AngularJS
PDF
Devoxx uk 2014 High performance in-memory Java with open source
ODP
* DJANGO - The Python Framework - Low Kian Seong, Developer
PDF
Getting started with React 16
PDF
Aligning Ember.js with Web Standards
PDF
GraphQL vs Traditional Rest API
PDF
Building Desktop RIAs With PHP And JavaScript
PDF
Selenium&amp;scrapy
PDF
Data Migrations in the App Engine Datastore
PPT
Django and Mongoengine
PDF
Django Mongodb Engine
ODP
Adding To the Leaf Pile
PDF
Server Side Swift
PPT
Parceable serializable
PDF
Django Introduction & Tutorial
PDF
QTP Descriptive programming Tutorial
PDF
React mit TypeScript – eine glückliche Ehe
Object Oriented Javascript
JS Fest 2018. Сергей Пузанков. E2E-тестирование фронтенда c Hermione
Restful App Engine
Create responsive websites with Django, REST and AngularJS
Devoxx uk 2014 High performance in-memory Java with open source
* DJANGO - The Python Framework - Low Kian Seong, Developer
Getting started with React 16
Aligning Ember.js with Web Standards
GraphQL vs Traditional Rest API
Building Desktop RIAs With PHP And JavaScript
Selenium&amp;scrapy
Data Migrations in the App Engine Datastore
Django and Mongoengine
Django Mongodb Engine
Adding To the Leaf Pile
Server Side Swift
Parceable serializable
Django Introduction & Tutorial
QTP Descriptive programming Tutorial
React mit TypeScript – eine glückliche Ehe
Ad

Similar to Agile web development Groovy Grails with Netbeans (20)

PPT
Grails Introduction - IJTC 2007
ODP
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
ODP
Groovygrailsnetbeans 12517452668498-phpapp03
PPT
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
ODP
ActiveWeb: Chicago Java User Group Presentation
PPT
Introduction To Groovy 2005
PPTX
Grails Plugin
PDF
Intro To Mvc Development In Php
ODP
This upload requires better support for ODP format
PDF
HTML5 for the Silverlight Guy
PPT
Groovy & Grails: Scripting for Modern Web Applications
PPT
Introduction To Grails
PPTX
Groovy Grails Gr8Ladies Women Techmakers: Minneapolis
PPT
GWT Extreme!
ODP
SVCC Intro to Grails
PPT
Direct Web Remoting : DWR
PPT
Mongo-Drupal
PPT
Boosting Your Testing Productivity with Groovy
PPT
Javaone2008 Bof 5101 Groovytesting
PPT
Ratpack - Classy and Compact Groovy Web Apps
Grails Introduction - IJTC 2007
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
Groovygrailsnetbeans 12517452668498-phpapp03
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
ActiveWeb: Chicago Java User Group Presentation
Introduction To Groovy 2005
Grails Plugin
Intro To Mvc Development In Php
This upload requires better support for ODP format
HTML5 for the Silverlight Guy
Groovy & Grails: Scripting for Modern Web Applications
Introduction To Grails
Groovy Grails Gr8Ladies Women Techmakers: Minneapolis
GWT Extreme!
SVCC Intro to Grails
Direct Web Remoting : DWR
Mongo-Drupal
Boosting Your Testing Productivity with Groovy
Javaone2008 Bof 5101 Groovytesting
Ratpack - Classy and Compact Groovy Web Apps
Ad

More from Carol McDonald (20)

PDF
Introduction to machine learning with GPUs
PDF
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
PDF
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
PDF
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
PDF
Predicting Flight Delays with Spark Machine Learning
PDF
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
PDF
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
PDF
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
PDF
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
PDF
How Big Data is Reducing Costs and Improving Outcomes in Health Care
PDF
Demystifying AI, Machine Learning and Deep Learning
PDF
Spark graphx
PDF
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
PDF
Streaming patterns revolutionary architectures
PDF
Spark machine learning predicting customer churn
PDF
Fast Cars, Big Data How Streaming can help Formula 1
PDF
Applying Machine Learning to Live Patient Data
PDF
Streaming Patterns Revolutionary Architectures with the Kafka API
PPTX
Apache Spark Machine Learning Decision Trees
PDF
Advanced Threat Detection on Streaming Data
Introduction to machine learning with GPUs
Streaming healthcare Data pipeline using Apache APIs: Kafka and Spark with Ma...
Analyzing Flight Delays with Apache Spark, DataFrames, GraphFrames, and MapR-DB
Analysis of Popular Uber Locations using Apache APIs: Spark Machine Learning...
Predicting Flight Delays with Spark Machine Learning
Structured Streaming Data Pipeline Using Kafka, Spark, and MapR-DB
Streaming Machine learning Distributed Pipeline for Real-Time Uber Data Using...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real-Ti...
Applying Machine Learning to IOT: End to End Distributed Pipeline for Real- T...
How Big Data is Reducing Costs and Improving Outcomes in Health Care
Demystifying AI, Machine Learning and Deep Learning
Spark graphx
Applying Machine learning to IOT: End to End Distributed Distributed Pipeline...
Streaming patterns revolutionary architectures
Spark machine learning predicting customer churn
Fast Cars, Big Data How Streaming can help Formula 1
Applying Machine Learning to Live Patient Data
Streaming Patterns Revolutionary Architectures with the Kafka API
Apache Spark Machine Learning Decision Trees
Advanced Threat Detection on Streaming Data

Agile web development Groovy Grails with Netbeans

  • 1. Agile Web Development with Groovy and Grails Carol McDonald, Java Architect
  • 2. Objective Overview of the Grails Web Platform
  • 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
  • 5. A Valid Java Program import 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(&quot;Ted&quot;); names.add(&quot;Fred&quot;); names.add(&quot;Jed&quot;); names.add(&quot;Ned&quot;); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); } }
  • 6. A Valid Groovy Program import 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(&quot;Ted&quot;); names.add(&quot;Fred&quot;); names.add(&quot;Jed&quot;); names.add(&quot;Ned&quot;); System.out.println(names); Erase e = new Erase(); List shortNames = e.filterLongerThan(names, 3); System.out.println(shortNames); } }
  • 7. The Groovy Way def names = [&quot;Ted&quot;, &quot;Fred&quot;, &quot;Jed&quot;, &quot;Ned&quot;] println names def shortNames = names.findAll{ it.size() <= 3 } print shortNames
  • 9. What is Grails? An Open Source Groovy MVC framework for web applications Principles CoC – Convention over configuration DRY – Don't repeat yourself Similar to RoR but with tighter integration to the Java platform
  • 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
  • 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 Add bin to your PATH $GROOVY_HOME/bin:$GRAILS_HOME/bin For command line
  • 14. grails create-app Will be prompted for the name of the application Generate a directory structure for Grails source Additional libraries Configurations web-app IDE runs the &quot;grails create-app&quot; command Demo
  • 15. Grails Directory Structure Application name Additional JARs Web application Grail source
  • 16. Netbeans Grails Project Structure Project name Grail source Additional JARs Web application
  • 17. Configure for MySQL copy the mysql-connector-java-5.1.6-bin.jar to the lib directory Edit DataSource.groovy dataSource { pooled = true driverClassName = &quot;com.mysql.jdbc.Driver&quot; username = &quot;root&quot; password = &quot;&quot; dialect = &quot;org.hibernate.dialect.MySQL5InnoDBDialect&quot; } environments { development { dataSource { dbCreate = &quot;update&quot; // one of 'create', 'create-drop','update' url = &quot;jdbc:mysql://localhost/petcatalog&quot; } }
  • 18. MVC and Grails Models, or domain classes, represent the problem domain
  • 19. grails create-domain-class The Model is your application's persistent business domain objects.
  • 20. add the domain class attributes class 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.
  • 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 property def item = new Item(name:'Fred') def order = new Order(orderDate: new Date(), item: 'PSP') item. addToOrders (order) item. save ()
  • 22. Queries Dynamically generated queries list , findBy Manual queries (not discussed here) HibernateQL Examples Item.list() Item.findBy Name ('nice cat')
  • 23. grails domain class Define relation between objects with attributes hasMany , belongsTo static hasMany = [ item: Item ] Groovy hashmap Address String street String zip static hasMany = [item:Item] 1 M Item String name String description Address address
  • 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
  • 25. Scaffolding Generates a controller and views for the domain class Demo
  • 26. MVC and Grails Controllers control request flow, interact with models, and delegate to views.
  • 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 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. URL Mappings Default mapping from URL to action method http://host/catalog/item/list Defined in grails-app/conf/UrlMappings.groovy static mappings = { &quot;/$controller/$action ? /$id ? &quot; Application Controller Action
  • 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. MVC and Grails Views are defined in Groovy Server Pages (GSP) and render the model
  • 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> <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. grails run-app Will start the embedded web server and run the application Defaults to http://localhost:8080/<app_name> Demo
  • 33. Summary Groovy is a powerful dynamic language for the JVM Grails is a full featured web platform
  • 35. Acknowldegement Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides
  • 36. Presenter’s Name [email_address] Carol McDonald Java Architect Tech Days 2009
  • 37. Lots of Books and Online Tutorials