Grails Introduction - IJTC 2007

2,800 views

Published on

Introduction to the Grails web framework at the Irish Java Technology Conference 2007

Published in: Technology, Education
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
2,800
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
163
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Grails Introduction - IJTC 2007

  1. 1. Grails Web app development with pleasure! Guillaume Laforge Vice-President, Technology G2One, Inc. http://www.g2one.com
  2. 2. Goal of This Talk Discovering the Grails web framework <ul><ul><li>Learn more about Grails, how easily you can use it in your projects, and how to get back the pleasure of web development! </li></ul></ul>
  3. 3. Guillaume Laforge <ul><li>Groovy Project Manager </li></ul><ul><li>JSR-241 Spec Lead </li></ul><ul><li>Initiator of the Grails framework </li></ul><ul><li>Co-author of Groovy in Action </li></ul><ul><ul><li>By Dierk König, et al. </li></ul></ul><ul><li>Vice-President Technology </li></ul>
  4. 4. Agenda <ul><li>What’s Grails? </li></ul><ul><li>The Problem with the usual web frameworks </li></ul><ul><li>The different layers </li></ul><ul><li>Other cool features </li></ul><ul><li>The plugin system </li></ul><ul><li>Summary </li></ul><ul><li>Q&A </li></ul>
  5. 5. What’s Grails? <ul><li>From 10,000 feet </li></ul><ul><li>From 1,000 feet </li></ul><ul><li>Near the ground </li></ul>
  6. 6. What’s Grails? (1/3) <ul><li>From 10,000 feet </li></ul><ul><li>Grails is an MVC action-based framework </li></ul><ul><li>Principles </li></ul><ul><ul><li>CoC : Convention over Configuration </li></ul></ul><ul><ul><li>DRY : Don’t Repeat Yourself </li></ul></ul><ul><li>The essence of Ruby on Rails, but with the tight integration with the Java ecosystem </li></ul><ul><ul><li>Protect your investment! </li></ul></ul>
  7. 7. What’s Grails? (2/3) <ul><li>From 1,000 feet </li></ul>= + +
  8. 8. What’s Grails? (3/3) <ul><li>Near the ground… </li></ul><ul><li>Grails is built on proven & solid OSS bricks </li></ul><ul><ul><li>Spring : IoC, DI, Spring MVC, transactions… </li></ul></ul><ul><ul><li>Hibernate : ORM, querying mechanism </li></ul></ul><ul><ul><li>Groovy : for everything that matters </li></ul></ul><ul><ul><li>SiteMesh : page layout and composition </li></ul></ul><ul><ul><li>Quartz : for job scheduling </li></ul></ul><ul><ul><li>AJAX : integration with different libraries </li></ul></ul><ul><ul><li>Jetty & HSQLDB : for fast development cycles </li></ul></ul>
  9. 9. The Grails Stack
  10. 10. Why Groovy? <ul><li>Java-like syntax </li></ul><ul><ul><li>Flat learning curve </li></ul></ul><ul><li>The same programming models </li></ul><ul><ul><li>Same OO, security, threading models </li></ul></ul><ul><li>The same libraries </li></ul><ul><ul><li>JDK, and any in-house or OSS JAR </li></ul></ul><ul><li>And Groovy has… </li></ul><ul><ul><li>Closures, properties, malleable syntax for DSLs </li></ul></ul>
  11. 11. The Problem <ul><li>What’s the problem with Web frameworks? </li></ul><ul><li>Why has it got to be complex? </li></ul><ul><li>What are the pain points? </li></ul>
  12. 12. Has it got to be complex? <ul><ul><li>But it’s slow to start with </li></ul></ul><ul><ul><ul><li>Seting up the project takes time </li></ul></ul></ul><ul><ul><li>It gets complicated pretty rapidly </li></ul></ul><ul><ul><ul><li>Dive into Spring & Hibernate to wire everything together </li></ul></ul></ul><ul><ul><li>There are so many layers </li></ul></ul><ul><ul><ul><li>DAOs, DTOs, more abstraction layers </li></ul></ul></ul><ul><ul><li>Too many configuration files </li></ul></ul><ul><ul><ul><li>Often too much XML for everything </li></ul></ul></ul><ul><li>Struts / Spring / Hibernate is okay… </li></ul>
  13. 13. The Pain Points <ul><li>ORM persistence overly hard to master and get right </li></ul><ul><li>Numerous layers and configuration files lead to chaos </li></ul><ul><li>Ugly JSPs with scriptlets and the complexity of JSP tags </li></ul><ul><li>Grails addresses the fundamental flaws in Java web application development today without compromising the platform </li></ul>
  14. 14. The Different Layers <ul><li>Bottom-up! </li></ul><ul><ul><li>Transparent persistence </li></ul></ul><ul><ul><li>Controllers and services </li></ul></ul><ul><ul><li>Groovy Server Pages, templates & taglibs </li></ul></ul>
  15. 15. Grails’ MVC at a Glance… <ul><li>Model </li></ul><ul><ul><li>GORM: Grails Object Relational Mapping </li></ul></ul><ul><li>Controller </li></ul><ul><ul><li>Multi-action controller </li></ul></ul><ul><ul><li>Also the Service layer & Quartz job scheduling </li></ul></ul><ul><li>View </li></ul><ul><ul><li>GSP: Groovy Server Pages </li></ul></ul><ul><ul><li>Tag libraries </li></ul></ul>
  16. 16. Pain Point #1 – Peristence <ul><li>ORM is quite hard to master and get right </li></ul><ul><li>Many configuration files </li></ul>ibatis.xml hibernate.cfg.xml persistence.xml ejb-cmp.xml
  17. 17. GORM – Grails Object Relational Mapping <ul><li>Hibernate under the hood </li></ul><ul><li>Domain model is a set of POGOs </li></ul><ul><ul><li>Plain Old Groovy Objects </li></ul></ul><ul><ul><li>POGOs are transparently mapped! </li></ul></ul><ul><li>No hibernate.cfg.xml </li></ul><ul><ul><li>But can be overriden if needed </li></ul></ul><ul><li>All domain classes get useful instance and static methods for free </li></ul><ul><ul><li>Book.count(), Book.list(), Book.get(id)… </li></ul></ul><ul><ul><li>book.save(), book.delete(), book.update()… </li></ul></ul>
  18. 18. Example <ul><li>Grails provides a default ORM mapping strategy for Hibernate </li></ul>// A Book domain class class Book { String title Date releaseDate static belongsTo = [author: Author] } // A one-to-many class User { String name static hasMany = [bookmarks: Bookmark] } 18 8 Dec 2007 Groovy Recipes 17 2 11 Dec 2006 Groovy in Action 16 author_id release_date title id Dierk König 2 name id
  19. 19. <DEMO/>
  20. 20. Constraints <ul><li>Constraints are added in domain classes through a static constraint field: </li></ul><ul><ul><li>static constraint = { isbn(matches: &quot;[0-9]{9}[0-9X]&quot;) } </li></ul></ul><ul><li>Many constraints available: </li></ul><ul><ul><li>blank, creditcard, email, blank, nullable, matches, range, unique, url… </li></ul></ul><ul><li>You can create your own validator </li></ul><ul><ul><li>myField(validator: { it % 2 == 0 }) </li></ul></ul>
  21. 21. No more DAOs! <ul><li>Dynamic finder methods </li></ul><ul><ul><li>Book. findBy Title (&quot;The Stand&quot;) Book.findBy Title Like (&quot;Harry Pot%&quot;) Book.findBy ReleaseDate Between (start, end) Book.findBy Title LikeOr ReleaseDate LessThan ( &quot;%Grails%&quot;, someDate) </li></ul></ul><ul><li>Find by relationship </li></ul><ul><ul><li>Book.findAllByAuthor( Author.get(1) ) </li></ul></ul><ul><li>Affect sorting </li></ul><ul><ul><li>Book.findAllByAuthor( me, [sort: ‘title’, order: ‘asc’ ] ) </li></ul></ul>
  22. 22. Querying <ul><li>Query by example </li></ul><ul><ul><li>Book.find ( new Book(title: ‘The Shining’) ) </li></ul></ul><ul><li>HQL queries </li></ul><ul><ul><li>Book.find( &quot; from Book b where b.title like ‘Lord of%’ &quot; ) </li></ul></ul><ul><ul><li>Book.find( &quot; from Book b where b.title like ? &quot;, [ ‘Lord of%’ ] ) </li></ul></ul><ul><li>Criteria builder </li></ul><ul><ul><li>def results = Account.createCriteria() { like (&quot;holderFirstName&quot;, &quot;Fred%&quot;) and { between (&quot;balance&quot;, 500, 1000) eq (&quot;branch&quot;, &quot;London&quot;) } order (&quot;holderLastName&quot;, &quot;desc&quot;) }.list() </li></ul></ul>
  23. 23. Pain Point #2 – Services, Nav. & Pres. Logic <ul><li>Numerous layers </li></ul><ul><li>Conf file chaos </li></ul>web.xml xwork.xml applicationContext.xml sitemesh.xml struts-config.xml validator.xml faces-config.xml tiles.xml
  24. 24. Controllers <ul><li>class Book Controller { </li></ul><ul><li>def index = { redirect(action:list,params:params) } </li></ul><ul><li>def list = { [ bookList: Book.list( params ) ] } </li></ul><ul><li>def show = { [ book : Book.get( params.id ) ] } </li></ul><ul><li>def edit = { </li></ul><ul><li>def book = Book.get( params.id ) </li></ul><ul><li>if(!book) { </li></ul><ul><li>flash.message = &quot;Book ${params.id} not found&quot; </li></ul><ul><li>redirect(action:list) </li></ul><ul><li>} else return [ book : book ] </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>http://localhost:8080/myapp/ book / show </li></ul>
  25. 25. Services & Scheduled Tasks <ul><li>Services are Groovy classes that should contain your business logic </li></ul><ul><ul><li>Automatic injection of services in controllers & services simply by declaring a field: </li></ul></ul><ul><ul><li>class BookController { MySuperService mySuperService } </li></ul></ul><ul><li>Recuring events (Quartz) </li></ul><ul><ul><li>Intervals, or cron definitions </li></ul></ul><ul><ul><li>class MyJob { def cronExpression = &quot;0 0 24 * * ?&quot; def execute() { print &quot;Job run!&quot; } } </li></ul></ul>
  26. 26. Pain Point #3 – The View Layer <ul><li>JSP cluttered with scriptlets </li></ul><ul><li>Taglibs are painful </li></ul>c.tld fmt.tld spring.tld grails.tld struts.tld
  27. 27. The view layer <ul><li>Spring MVC under the hood </li></ul><ul><li>Support for flash scope between requests </li></ul><ul><li>GSP : Groovy alternative to JSP </li></ul><ul><li>Dynamic taglib development: </li></ul><ul><ul><li> no TLD, no configuration, just conventions </li></ul></ul><ul><li>Adaptive AJAX tags </li></ul><ul><ul><li>Yahoo, Dojo, Prototype </li></ul></ul><ul><li>Customisable layout with SiteMesh </li></ul><ul><li>Page fragments through reusable templates </li></ul><ul><li>Views under grails-app/views </li></ul>
  28. 28. Groovy Server Pages <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><meta name=&quot;layout&quot; content=&quot;main&quot; /> </li></ul><ul><li><title>Book List</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><a href=&quot; ${createLinkTo(dir:'')} &quot;>Home</a> </li></ul><ul><li><g:link action=&quot;create&quot;> New Book</g:link> </li></ul><ul><li><g:if test=&quot;${flash.message}&quot;> </li></ul><ul><li>${flash.message} </li></ul><ul><li></g:if> </li></ul><ul><li><g:each in=&quot; ${bookList} &quot;> ${it.title} </g:each> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  29. 29. Dynamic Tag Libraries <ul><li>Logical : if, else, elseif </li></ul><ul><li>Iterative : while, each, collect, findAll… </li></ul><ul><li>Linking : link, createLink, createLinkTo </li></ul><ul><li>Ajax : remoteFunction, remoteLink, formRemote, submitToRemote… </li></ul><ul><li>Form : form, select, currencySelect, localeSelect, datePicker, checkBox… </li></ul><ul><li>Rendering : render*, layout*, paginate… </li></ul><ul><li>Validation : eachError, hasError, message </li></ul><ul><li>UI : richTextEditor… </li></ul>
  30. 30. Write your Own Taglib <ul><li>Yet another Grails convention </li></ul><ul><ul><li>class My TagLib { def isAdmin = { attrs , body -> def user = attrs['user'] if(user != null && checkUserPrivs(user)) body() } } </li></ul></ul><ul><li>Use it in your GSP </li></ul><ul><ul><li><g:isAdmin user=&quot;${myUser}&quot;> some restricted content </g:isAdmin> </li></ul></ul>
  31. 31. What else? <ul><li>Custom URL mapping </li></ul><ul><li>Conversation flows </li></ul><ul><li>ORM DSL </li></ul><ul><ul><li>http://grails.org/GORM+-+ Mapping +DSL </li></ul></ul><ul><li>Develop with pleasure with IntelliJ IDEA </li></ul><ul><ul><li>http:// grails.org /IDEA+ Integration </li></ul></ul>
  32. 32. Custom URL Mappings (1/2) <ul><li>Pretty URLs in seconds </li></ul><ul><li>Custom DSL for mapping URLs to controllers, actions and views </li></ul><ul><li>Supports mapping URLs to actions via HTTP methods for REST </li></ul><ul><li>Supports rich constraints mechanism </li></ul>
  33. 33. Custom URL Mappings (2/2) <ul><li>Pretty URLs in seconds! </li></ul><ul><li>class UrlMappings { static mappings = { &quot;/product/ $id &quot;(controller: &quot;product&quot;, action: &quot;show&quot;) &quot;/$blog/ $year? /$month?/$day?&quot;(controller: &quot;blog&quot;, action: &quot;show&quot;) { constraints { year(matches: /d{4}/) } } } </li></ul>
  34. 34. Conversations flows (1/2) <ul><li>Leverages Spring WebFlow </li></ul><ul><li>Supports Spring’s scopes </li></ul><ul><ul><li>Request </li></ul></ul><ul><ul><li>Session </li></ul></ul><ul><ul><li>Conversation </li></ul></ul><ul><ul><li>Flash </li></ul></ul><ul><li>Possibly to specify services’ scope </li></ul><ul><li>DSL for constructing flows </li></ul>
  35. 35. Conversations flows (2/2) <ul><li>Example: </li></ul><ul><li>def searchFlow = { displaySearchForm { on(&quot;submit&quot;).to &quot;executeSearch&quot;   } executeSearch { action { [results: searchService. executeSearch(params.q)] } on(&quot;success&quot;).to &quot;displayResults&quot; on(&quot;error&quot;).to &quot;displaySearchForm&quot;  } displayResults() } </li></ul>
  36. 36. Grails plugin system <ul><li>Beyond the out-of-the-box experience, </li></ul><ul><li>extend Grails with plugins </li></ul><ul><li>and write your own! </li></ul>
  37. 37. Plugin Extension Points <ul><li>Extend Grails beyond what it offers! </li></ul><ul><li>What can you do with plugins? </li></ul><ul><ul><li>Hook into the build system </li></ul></ul><ul><ul><li>Script the Spring application context </li></ul></ul><ul><ul><li>Register new dynamic methods </li></ul></ul><ul><ul><li>Container configuration (web.xml) </li></ul></ul><ul><ul><li>Adding new artefacts types </li></ul></ul><ul><ul><li>Auto-reload your artefacts </li></ul></ul>
  38. 38. Plenty of Plugins! <ul><li>XFire </li></ul><ul><ul><li>Expose Grails services as SOAP </li></ul></ul><ul><li>Searchable </li></ul><ul><ul><li>Integrate Lucene & Compass search </li></ul></ul><ul><li>Remoting </li></ul><ul><ul><li>Expose Grails services over RMI, Burlap, or REST </li></ul></ul><ul><li>Google Web Toolkit </li></ul><ul><ul><li>Integrate Grails with GWT for the presentation layer </li></ul></ul><ul><li>Acegi / JSecurity </li></ul><ul><ul><li>Secure your Grails app </li></ul></ul><ul><li>JMS </li></ul><ul><ul><li>Expose services as message-driven beans </li></ul></ul>
  39. 39. Sweet spot: Enterprise-readiness <ul><li>Skills, libraries, app servers… </li></ul>
  40. 40. Protect your Investment <ul><li>Reuse </li></ul><ul><ul><li>Existing Java libraries (JDK, 3rd party, in-house) </li></ul></ul><ul><ul><li>Employee skills & knowledge (both prod & soft) </li></ul></ul><ul><ul><li>Spring configured beans </li></ul></ul><ul><ul><li>Hibernate mappings for legacy schemas (but still benefit from dynamic finders) </li></ul></ul><ul><ul><li>EJB3 annotated mapped beans </li></ul></ul><ul><ul><li>JSPs, taglibs for the view </li></ul></ul><ul><li>Deploy on your pricey Java app-server & database </li></ul><ul><li>Grails will fit in your Java EE enterprise architecture! </li></ul>
  41. 41. Let’s Wrap Up <ul><li>Summary </li></ul><ul><li>Resources </li></ul><ul><li>Q&A </li></ul>
  42. 42. Summary <ul><li>Groovy is a powerful dynamic language for the JVM, that provides agile development on the Java platform, without the impedance mismatch of other languages </li></ul><ul><ul><li> Groovy 1.1 out next week </li></ul></ul><ul><li>Grails is a fully-featured web application framework based on proven technologies like Spring and Hibernate , which simplifies the development of innovative applications </li></ul><ul><ul><li> Grails 1.0 at the end of the month </li></ul></ul>
  43. 43. Resources <ul><li>Grails: http://grails.org </li></ul><ul><li>Groovy: http://groovy.codehaus.org </li></ul><ul><li>Groovy blogs: http://groovyblogs.org </li></ul><ul><li>AboutGroovy: http://aboutgroovy.com </li></ul><ul><li>Mailing-lists: http://grails.org/Mailing+lists </li></ul><ul><li>G2One: http://www.g2one.com </li></ul>
  44. 44. G2One, Inc. <ul><li>Groovy & Grails at the source! </li></ul><ul><ul><li>Graeme Rocher, Grails lead </li></ul></ul><ul><ul><li>Guillaume Laforge, Groovy lead </li></ul></ul><ul><ul><li>Plus key committers </li></ul></ul><ul><li>Professional Services </li></ul><ul><ul><li>Training, support, consulting </li></ul></ul><ul><ul><li>Custom developments </li></ul></ul><ul><ul><ul><li>Connectors, plugins, etc… </li></ul></ul></ul>
  45. 45. Q&A

×