Grails - Rapid Web Application Development for the Java Platform Mischa Kölliker Guido Schmutz Zürich, 24.06.2008 Basel Baden Bern Lausanne Zurich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg Munich Stuttgart Vienna
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 2 2008
What is Grails? Grails is to Groovy what Ruby on Rails is to Ruby aims to bring the "coding by convention" paradigm to Groovy open-source web application framework leverages the Groovy language and complements Java Web development Grails can be used as a standalone development environment that hides all configuration details or integrate your Java business logic Grails aims to make development as simple as possible Grails - Rapid Web Application Development for the Java Platform 3 2008
How Grails stacks up Grails Java EE Spring Hibernate Sitemesh Quartz Groovy Java Language Java Development Kit (JDK) Java Virtual Machine Grails - Rapid Web Application Development for the Java Platform 4 2008
How Grails stacks up Hibernate The de facto standard for ORM in the Java world Spring The hugely popular open source IoC container and wrapper framework for Java Quartz An enterprise-ready, job-scheduling framework allowing flexibility and durable execution of scheduled tasks SiteMesh A robust and stable layout-rendering framework Grails - Rapid Web Application Development for the Java Platform 5 2008
Getting started with Grails Installation in 5 steps 1. download from http://www.grails.org 2. extract zip 3. Set GRAILS_HOME variable 4. Add $GRAILS_HOME\bin to PATH variable 5. run "grails help" Grails - Rapid Web Application Development for the Java Platform 6 2008
Command Line Apache Ant bundled with Grails Many useful targets available: create-* (for creating Grails artifacts) generate-controller generate-views run-app test-app run-webtest help You will never see an Ant build.xml Everything is written as "Gant" script Grails - Rapid Web Application Development for the Java Platform 7 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 8 2008
1) Creating the initial Application C:\temp>grails create-app wineshopdemo Make sure you are in a clean directory The name will be used for the URL the WAR file that gets generate... Convention over configuration Can be overwritten in application.properties file Grails - Rapid Web Application Development for the Java Platform 9 2008
2) Exploring the Directory Structure C:\temp>cd wineshopdemo Grails uses a standard directory structure like Maven, Rails or AppFuse Models, Views, Controllers all of the interesting bits of the application Custom JARS such as DB drivers (WEB-INF/lib) Custom Groovy Scripts Java source files to be compiled (WEB-INF/classes) Unit and integration tests GSPs, CSS, JavaScript and other traditional web files Grails - Rapid Web Application Development for the Java Platform 10 2008
3) Creating a Domain Class C:\temp\wineshopDemo>grails create-domain-class Customer Results in two files Customer.groovy in grails-app/domain class Customer { } CustomerTests.groovy in test/integration class CustomerTests extends GroovyTestCase { void testsomething() { } } Every file in grails-app/domain gets persisted to a database automatically Grails - Rapid Web Application Development for the Java Platform 11 2008
4) Adding Fields to the Domain Class class Customer { String title String firstname String lastname } String tostring() { return "$firstname $lastname" } The domain classes in Grails are GroovyBeans, plain and simple POGO (plain old groovy object) They get more than just automatic getters and setters instance methods customer.save() and customer.delete() static methods such as Customer.get() and Customer.list() Additional fields like id and version Other methods that don't exist like Customer.findByFirstName("Paul") Grails - Rapid Web Application Development for the Java Platform 12 2008
5) Create the Controller and enable scaffolding C:\temp\wineshopDemo>grails create-controller Customer will create a new controller in the grails-app/controllers directory called CustomerController.groovy Scaffolding allows to auto-generate a whole application for a given domain class including The necessary views Controller actions for create/read/update/delete (CRUD) operations Enable scaffolding via the scaffold property class CustomerController { class CustomerController { } def index = { } } def scaffold = true Grails - Rapid Web Application Development for the Java Platform 13 2008
6) Configuration Changing Database Grails allows developing without any additional configuration Grails ships with an embedded container and in-memory HSQLDB to set-up a real database you need to change the defaults and copy the JDBC driver the lib folder datasource { pooled = false driverclassname = "oracle.jdbc.oracledriver" username = "winedemo" password = "winedemo" }... // environment specific settings environments { development { datasource { dbcreate = "update" url = "jdbc:oracle:thin:@localhost:1521:xe" }... Grails - Rapid Web Application Development for the Java Platform 14 2008
7) Starting the Application C:\temp\wineshopDemo>grails run-app Launches the embedded version of Jetty on the default port 8080 to use another port, add server.port property C:\temp\wineshopDemo>grails run-app Dserver.port=8092 After Jetty starts, Grails scans the grails-app/domain folder and creates the necessary tables (depending on configuration) Grails - Rapid Web Application Development for the Java Platform 15 2008
8) Using the application Grails - Rapid Web Application Development for the Java Platform 16 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 17 2008
WineshopDemo Domain Model class Class Model Account - username: String - password: String - active: Boolean +user 0..1 1 Customer - title: String - firstname: String - lastname: String - dateofbirth: java.util.date - phonenumbe r: String - email: String - homepageurl: String +customers 0..* +rating 0..1 Rating - code: String - name: String +customer 1 1..* +addresses {ordered} Address - category: String - street: String - zipcode: String - city: String + fmtpostaladdress() : String +country Country - code: String - name: String Grails - Rapid Web Application Development for the Java Platform 18 2008
Changing the Field Order and Validating the Data With a static constraints block you can control the field order add validation to the fields class Customer {... static constraints = { title(nullable:false, inlist:["herr","frau"]) firstname(nullable:true) lastname(nullable:true) phonenumber(nullable:true) email(nullable:true, email:true) homepageurl(nullable:true, url:true) dateofbirth(nullable:true) } order Methods such as customer.save() validate the object before saving it Grails - Rapid Web Application Development for the Java Platform 19 2008
Validating the Data: Standard validations nullable Ensures the property value cannot be null blank Prevents empty fields email Checks for well-formed email addresses inlist Displays a combo-box unique Prevents duplicate values in the database min, max Minimum, maximum value for a numeric field minsize, maxsize Minimum, maximum length for a text field matches Applies a regular expression against a string value validator Set to a closure or block to use as a custom validation Grails - Rapid Web Application Development for the Java Platform 20 2008
class Class Model Managing Relationships Account - username: String - password: String - active: Boolean +user 0..1 1 Customer - title: String - firstname: String - lastname: String - dateofbirth: java.util.date - phonenumbe r: String - email: String - homepageurl: String +customers 0..* Rating +rating - code: String 0..1 - name: String class Customer {... // one-to-one Account account // many-to-one static belongsto = [rating:rating] +customer 1 Address - category: String - street: String - zipcode: String - city: String 1..* +addresses {ordered} + fmtpostaladdress() : String +country Country - code: String - name: String // one-to-many static hasmany = [addresses:address] class Address {... static belongsto = [customer:customer, country:country] class Rating {... static hasmany = [customers:customer] Grails - Rapid Web Application Development for the Java Platform 21 2008
Custom ORM mappings Table name class Customer {... static mapping = { table 'customer_t' } Column Name class Customer {... static mapping = { table 'customer_t' phonenumber column:'phone_number' } Grails - Rapid Web Application Development for the Java Platform 22 2008
Querying with GORM List all instances or retrieve by id def list = Customer.list(sort: lastname ) def cust = Customer.get(1) class Customer { String title String firstname String lastname Date dateofbirth String phonenumber String email String homepageurl GORM supports the concept of dynamic finders looks like a static method invocation, but the methods don't actually exist def results = Customer.findByFirstName("Andrea") results = Customer.findByFirstNameLike("A%") results = Customer.findByDateOfBirthBetween(fromDate, todate) results = Customer.findAllByEmailIsNull() results = Customer.findByFirstNameIlikeAndDateOfBirthLessThan("a%", new Date()) Hibernate Query Language (HQL) def results = Customer.findAll("from Customer as c where c.firstname like 'G%'") Grails - Rapid Web Application Development for the Java Platform 23 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 24 2008
Controllers and views Grails - Rapid Web Application Development for the Java Platform 25 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 26 2008
Controllers Every domain has it's Controller Controllers consist mainly of a set o action methods (Closures) Lots of implicit objects and methods are injected at runtime. To explore them, start here: org.codehaus.groovy.grails.plugins.web.controllersgrailsplugin org.codehaus.groovy.grails.web.metaclass.renderdynamicmethod Action methods return a model, which is used by the view The model is normally a map If no model is returned, the Controller itself acts as a model The name of the action is used to find a view with the same name Can be overridden Grails - Rapid Web Application Development for the Java Platform 27 2008
Controllers: Output formats Grails - Rapid Web Application Development for the Java Platform 28 2008
Scopes injected to Controllers servletcontext, session, request as usual params a mutual map of request parameters flash a scope living in this and the next request (across redirect) Scope objects are dynamically injected into controllers Controllers itself are prototype-scoped, i.e. live only during one request (and thus are thread safe) Grails - Rapid Web Application Development for the Java Platform 29 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 30 2008
The View: GSP Groovy Server Pages Nearly the same as JSP, but Support Groovy as content of EL expressions More lightweight no compilation Fewer options Templating like Facelets Support Groovy tag libraries Grails - Rapid Web Application Development for the Java Platform 31 2008
Layouting Grails - Rapid Web Application Development for the Java Platform 32 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 33 2008
TagLibs: The Use Default namespace is g: Grails - Rapid Web Application Development for the Java Platform 34 2008
TagLibs: The Library The name of the Closure is the name of the tag No TLD, nor registration anywhere Grails - Rapid Web Application Development for the Java Platform 35 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 36 2008
Generating RSS-Feeds Grails - Rapid Web Application Development for the Java Platform 37 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 38 2008
Grails WebServices REST and SOAP style SOAP supported via XFire and Axis2 plugins (among others) REST supported via URL mapping Groovy features Grails - Rapid Web Application Development for the Java Platform 39 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Overview Controllers View / GSP TagLibs RSS WebServices Plugins Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 40 2008
Currently about 60 Plugins Grails - Rapid Web Application Development for the Java Platform 41 2008
Grails Plugins > grails list-plugins > grails install-plugin <name> > grails plugin-info <name> > grails create-plugin Plugins are themeselves Grails applications Plugins are provided with several hooks during install Plugin resources are added to the main application at startup Grails - Rapid Web Application Development for the Java Platform 42 2008
Grails Plugins Grails - Rapid Web Application Development for the Java Platform 43 2008
RichUI: DateChooser Grails - Rapid Web Application Development for the Java Platform 44 2008
RichUI: Flow Grails - Rapid Web Application Development for the Java Platform 45 2008
RichUI: Drag & Drop Grails - Rapid Web Application Development for the Java Platform 46 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 47 2008
Agenda What is Grails? 8 steps to a Grails Application Domain Modeling in GORM Data are always part of the game. Grails Controllers and Groovy Server Pages Grails Applications Summary Grails - Rapid Web Application Development for the Java Platform 48 2008
Summary With the advent on Web 2.0 agility is key Dynamic frameworks (Grails, Rails, Django etc.) provide this through quick iterative development with a clear productivity gain However, for large scale applications static-typing and IDE support is crucial Grails provides the ability to use a blended approach And most importantly it runs on the JVM! Grails - Rapid Web Application Development for the Java Platform 49 2008
Thank you!? www.trivadis.com Basel Baden Bern Lausanne Zurich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg Munich Stuttgart Vienna