• No results found

Intruduction to Groovy & Grails programming languages beyond Java

N/A
N/A
Protected

Academic year: 2021

Share "Intruduction to Groovy & Grails programming languages beyond Java"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

Intruduction to

Groovy & Grails

(2)

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

(3)
(4)

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

(5)

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;

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 }

(6)

simplifying the code

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 }

example as Groovy class:

implicitly included packages

no semicolons necessary

simpler println statement

optional typing in line 09

(7)

further simplifying the

code

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 }

using Groovy's collection

and map notation

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)

(8)

getting rid of the main

method

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!

(9)

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

(10)

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

(11)

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

(12)
(13)

Grails architecture

database

data persistency layer

(hibernate) and database driver

REST resources

controllers and views (Groovy,

GSP)

Services

web browser or WS client

http

tcp/ip

presentation

tier

presentation

tier

business

tier

data access

tier

{

{

{

(14)

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

(15)

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

> grails compile

Proxy must

be set!

(16)

create domain classes

1st step: create two domain classes:

> grails create-domain-class mypac.Lecture

> grails create-domain-class mypac.Document

edit the generated domain classes to define the

data fields

Don't forget the

package!

(17)

domain class Lecture

class Lecture {

String name

String description

static constraints = { name(blank:false) }

String toString() {

name

}

static hasMany = [documents:Document]

}

insert here information about

lectures

(18)

domain class Document

class Document {

String name

String storageLocation

Lecture lecture

static constraints = { name(blank:false) }

String toString() {

name

}

static belongsTo = Lecture

}

insert here information about a

document belonging to a lecture

(19)

generate views and

domain controllers

create a complete application scaffold by issuing:

> grails generate-all "*"

run the application

grails run-app

(20)

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

(21)

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

(22)

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')

(23)

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

(24)

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 {

(25)

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

References

Related documents

Network Firewalls Do Not Work For HTTP Firewall Port 80 HTTP Traffic Web Client Web Server Application Application Database Server...

Six different possibilities were evaluated on acceptance by the respondents (general budget, new roads, improve public transport, abandon existing car taxation, lower fuel taxes,

The radiography control button was released before the exposure end. The display indicates the remaining exposure time. Based on this time, you must decide whether to develop

inthelungsln man.However,the present da11ghter rediae and cercariae resemblein morphology those of the triploid formobtainedinShimazu’S(1981)experiment and those PrObably of

Old Habit: Paper CRF Process Primary Investigator Source Document CRC CRFs CRFs DB1 Double Data Entry Master Clinical 6 Query Report Form Site Sponsor Query Report Form CDM

written as 'do not count towards the minimum written as 'do not count towards the minimum number of Core units you must include' or number of Core units you must include'

Kalandoor Career DMCC Careers Dubai Customs DP World Career Dalkia Dubai Career ADGAS Career Mattex Career [email protected]. Paris Gallery

By subtracting all the proteins that were identified as orthologs from the groups of paralogs and unique genes, we were left with only the protein pro- ducts of gene models that