Intruduction to Groovy & Grails programming languages beyond Java 1
Groovy, what is it? Groovy is a relatively new agile dynamic language for the Java platform exists since 2004 belongs to the family of modern easy to use scripting languages like Ruby, Python, or Pearl 2
Groovy s magic Groovy is simpler to write than Java advanced features allow for fewer lines of code Groovy relies on Java all Java APIs can be used with Groovy use Groovy to get rid of semicolons and other annoying must haves in Java see the power and elegance of Groovy ---> 4
rewriting Java code to Groovy 01 package hs-owl.korte.gex; 02 03 import java.util.list; 04 import java.util.arraylist; 05 06 07 public class Task { 08 private String name; 09 private String descr; 10 11 public Task() {} 12 13 public Task(String name, String descr) { 14 this.name = name; 15 this.descr = descr; 16 } 17 18 public String getname() { 19 return name; 20 } 21 22 public void setname(string name) { 23 this.name = name; 24 } 25 26 public String getdescr() { 27 return descr; 28 } 29 30 public void setdescr(string de) { 31 descr = de; 32 } 33 34 public static void main(string[] args) { 35 List tasks = new ArrayList(); 36 tasks.add(new Task("1", "first")); 37 tasks.add(new Task("2", "second")); 38 tasks.add(new Task("3","third")); 39 40 for(task ag : tasks) 41 System.out.println(ag.getName() + " " + ag.getdescr()); 42 } 43 } 44 }
simplifying the code example as Groovy class: 01 package hs-owl.korte.gex 02 03 public class Task { 04 05 private String name 06 private String descr 07 08 public static void main(string[] args) { 09 def tasks = new ArrayList() 10 tasks.add(new Task (name:"1", descr:"first")) 11 tasks.add(new Task (name:"2", descr:"second")) 12 tasks.add(new Task (name:"3", descr:"third")) 13 14 for(task ag : tasks) 15 println "$(ag.name) $(ag.descr)" 16 } 17 } 18 } implicitly included packages no semicolons necessary simpler println statement optional typing in line 09
further simplifying the code using Groovy's collection and map notation 01 package hs-owl.korte.gex 02 03 public class Task { 04 05 private String name 06 private String descr 07 08 public static void main(string[] args) { 09 def tasks = [ 10 new Task(name:"1", descr:"first"), 11 new Task(name:"2", descr:"second"), 12 new Task(name:"3", descr:"third") 13 ] 14 15 tasks.each { 16 println "$(it.name) $(it.descr)" 17 } 18 } 19 } for statement replaced with closure in line 09 closure = reusable block of code can be passed to methods, defined using keyword def ArrayList is expressed as [ ] closure is passed to each method, which is provided for collections (line 15)
getting rid of the main method example as Groovy script: 01 package hs-owl.korte.gex 02 03 public class Task { 04 05 private String name 06 private String descr 07 08 } 09 09 def tasks = [ 10 new Task(name:"1", descr:"first"), 11 new Task(name:"2", descr:"second"), 12 new Task(name:"3", descr:"third") 13 ] 14 15 tasks.each { 16 println "$(it.name) $(it.descr)" 17 } 18 } 19 } a script is also compiled to a class Compare this with the original Java code!
getting started download Groovy from http://groovy.codehaus.org/ read the Getting Started Guide provided there it contains the installation instructions read my "Basic Groovy" slides for a brief introduction and how Groovy differs from Java Groovy Slides
Grails, what is it? developing Web applications of the Web 2.0 category used to be a hard an complex task, because it involves a lot of technologies: HTML, CSS, AJAX, XML, Web services, Java, databases, and frameworks like JSP or JSF with Grails this has become much easier Grails is an open source web framework and development platform based on Groovy scipts
literature Groovy: http://groovy.codehaus.org/ C. M. Judd, J. F. Nusairat, J. Shingler: Beginning Groovy and Grails - From Novice to Professional. Apress, 2008 Grails http://www.grails.org/
Grails architecture { presentation tier business tier { data access tier { web browser or WS client controllers and views (Groovy, GSP) REST resources http Services data persistency layer (hibernate) and database driver tcp/ip database
Building a RESTful Web Service with Grails follow these 3 easy steps create domain classes describing the data update URL mappings to associate the http commands GET, POST, etc. with closures to be defined in the domain object controllers create domain controllers with closures to handle the http commands
an example example assignment: Write a Grails application named example for handling information about lectures and their associated learning documents. begin by creating an 'empty' grails application > grails create-app example > cd example Proxy must be set! > grails compile
create domain classes 1st step: create two domain classes: Don't forget the package! > grails create-domain-class mypac.lecture > grails create-domain-class mypac.document edit the generated domain classes to define the data fields
domain class Lecture class Lecture { String name String description insert here information about lectures static constraints = { name(blank:false) } String tostring() { } name } static hasmany = [documents:document]
domain class Document class Document { String name String storagelocation Lecture lecture insert here information about a document belonging to a lecture static constraints = { name(blank:false) } } String tostring() { name } static belongsto = Lecture
generate views and domain controllers create a complete application scaffold by issuing: > grails generate-all "*" run the application grails run-app
Modifying the Web Application to Return XML Data Instead of HTML Pages a web application denotes a client/server application using http as the transport protocol and HTML as the data format the client is a web browser a function (service) is selected via a HTML link a RESTful web service denotes the server side of a client/server application using http as the transport protocol and some machine readable data format the client is an application capable of reading and writing this data fomat using http commands - it may also be a web browser a function (service) is selected by forming a URL with a certain structure and using the right http command (GET, PUT, DELETE, POST)
planning the additional RESTful behavior the Web service should be access according to the URL schema: http://localhost:8080/example/rest/<controller/<id> i.e.: http://localhost:8080/rest/lecture/1 http://localhost:8080/rest/document/1
modifying URLMappings.groovy class UrlMappings { static mappings = { "/$controller/$action?/$id?"{ constraints { // apply constraints here } } "/"(view:"/index") "500"(view:'/error') // insert the following lines for a RESTful Web service "/rest/lecture/$id?"(controller:"lecture",action:"rest") "500"(view:'/error') "/rest/document/$id?"(controller:"document",action:"rest") "500"(view:'/error') } }
mapping actions to http operations http GET return details, when an id is given, return a list otherwise http POST create a lecture or a document http PUT modify a lecture or a document http DELETE delete a lecture or a document
include the rest action in the domain controllers def rest = { if (request.method == "GET") { if (params.id!= null) render Lecture.get(params.id) as JSON else render Lecture.list() as JSON } else if (request.method == "POST") { //... } else if (request.method == "PUT") { //... } else if (request.method == "DELETE") { //... } else { response.status = 406 } }
Lab Assignment write a Grails application as described in the assignment page Building a Grails Application with a RESTful Web Service Interface optional 8% bonus for < MIT-13 student mandatory LAB2 grading for MIT-13 students