Intruduction to Groovy & Grails programming languages beyond Java



Similar documents
Agile Development with Groovy and Grails. Christopher M. Judd. President/Consultant Judd Solutions, LLC

Grails - Rapid Web Application Development for the Java Platform

Dynamic website development using the Grails Platform. Joshua Davis Senior Architect Cognizant Technology Solutions

BDD FOR AUTOMATING WEB APPLICATION TESTING. Stephen de Vries

Building Web Services with Apache Axis2

The Google Web Toolkit (GWT): The Model-View-Presenter (MVP) Architecture Official MVP Framework

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com

Web Frameworks. web development done right. Course of Web Technologies A.A. 2010/2011 Valerio Maggio, PhD Student Prof.

WIRIS quizzes web services Getting started with PHP and Java

Grails 1.1. Web Application. Development. Reclaiming Productivity for Faster. Java Web Development. Jon Dickinson PUBLISHING J MUMBAI BIRMINGHAM

Web Services API Developer Guide

How To Build A Web App

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

Web Cloud Architecture

GUI and Web Programming

An introduction to creating JSF applications in Rational Application Developer Version 8.0

Server based signature service. Overview

YouTrack MPS case study

Customer Bank Account Management System Technical Specification Document

Building Java Servlets with Oracle JDeveloper

IT3503 Web Development Techniques (Optional)

Implementing Mobile Thin client Architecture For Enterprise Application

Web Service Testing. SOAP-based Web Services. Software Quality Assurance Telerik Software Academy

02267: Software Development of Web Services

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

Oracle WebLogic Foundation of Oracle Fusion Middleware. Lawrence Manickam Toyork Systems Inc

Course Name: Course in JSP Course Code: P5

How To Write A Web Server In Javascript

Web Development. Owen Sacco. ICS2205/ICS2230 Web Intelligence

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Security Testing For RESTful Applications

Networks and Services

Web Applications: Overview and Architecture

Google Web Toolkit (GWT) Architectural Impact on Enterprise Web Application

02267: Software Development of Web Services

1001ICT Introduction To Programming Lecture Notes

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Example. Represent this as XML

CUT YOUR GRAILS APPLICATION TO PIECES

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

General principles and architecture of Adlib and Adlib API. Petra Otten Manager Customer Support

MarkLogic Server. Reference Application Architecture Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Web development... the server side (of the force)

Framework as a master tool in modern web development

Programming Languages CIS 443

Database Migration Plugin - Reference Documentation

Rapid Application Development. and Application Generation Tools. Walter Knesel

The end. Carl Nettelblad

Certified PHP/MySQL Web Developer Course

Web Development on the SOEN 6011 Server

Modern Web Development From Angle Brackets to Web Sockets

Developing modular Java applications

Automatic generation of fully-executable code from the Domain tier of UML diagrams

System i Windows Integration Solutions

How to make a good Software Requirement Specification(SRS)

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

CAKEPHP & EXTJS - RESPONSIVE WEB TECHNOLOGIES

Kotlin for Android Developers

API documentation - 1 -

Table of contents. Reverse-engineers a database to Grails domain classes.

Lecture 2. Internet: who talks with whom?

Eclipse Web Tools Platform. Naci Dai (Eteration), WTP JST Lead

How to Build an E-Commerce Application using J2EE. Carol McDonald Code Camp Engineer

Building and Using Web Services With JDeveloper 11g

Performance Testing for Ajax Applications

ALERT & Cisco CallManager

Slides from INF3331 lectures - web programming in Python

branddocs Technology edocument Solutions V V

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

IT6503 WEB PROGRAMMING. Unit-I

LAMP [Linux. Apache. MySQL. PHP] Industrial Implementations Module Description

Overview of Web Services API

An introduction to creating Web 2.0 applications in Rational Application Developer Version 8.0

public void setusername(string username) { this.username = username; } public void setname(string name) { this.name = name; }

Automating Rich Internet Application Development for Enterprise Web 2.0 and SOA

Certified Selenium Professional VS-1083

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

DreamFactory Security Whitepaper Customer Information about Privacy and Security

Tableau Server Trusted Authentication

Forms, CGI Objectives. HTML forms. Form example. Form example...

SiteCelerate white paper

Module 4: File Reading. Module 5: Database connection

CTIS 256 Web Technologies II. Week # 1 Serkan GENÇ

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Grails: Accelerating J2EE Application Development

Instructor: Betty O Neil

A Tool for Evaluation and Optimization of Web Application Performance

How To Write An Ria Application

Transcription:

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