Grails 0.3-SNAPSHOT Presentation WJAX 2006 English


Published on

English Version. Audio will be published at

Published in: Technology, Business
1 Comment
  • Please note: since 0.3 final release, hasMany and belongsTo should be static properties in your domain classes! This does not mean, that all Books will have the same (static) Authors, as this iinformation s just used to inject the reference list (in this case 'authors' property).
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Grails 0.3-SNAPSHOT Presentation WJAX 2006 English

  2. 2. About: Sven Haiges <ul><li>Actionality Deutschland GmbH </li></ul><ul><li>Senior Java Developer </li></ul><ul><li>Diplom-Informatiker (FH), MBA </li></ul><ul><li>My personal framework evolution: Struts, JSF, Spring MVC, GRAILS </li></ul><ul><li>Since Juli 2006: Grails Podcast, Grails Screencasts </li></ul>
  3. 3. Goals <ul><li>What is Grails & how do I create my first Grails app? </li></ul><ul><li>What are the powerful cornerstones of Grails? </li></ul><ul><li>Which features does Grails provide out of the box? </li></ul><ul><li>Next Steps @ Home/Work </li></ul>
  4. 4. Agenda Basics Foundations MVC Features Roadmap Your Next Steps
  5. 5. Grails Basics
  6. 6. Grails MVC <ul><li>Grails is an MVC Web Framework </li></ul><ul><ul><li>Inspired by Ruby on Rails </li></ul></ul><ul><ul><li>Uses the powerful Groovy Scripting Language </li></ul></ul><ul><ul><li>Convention Over Configuration </li></ul></ul><ul><ul><li>Don't Repeat Yourself (DRY) </li></ul></ul>
  7. 7. Grails History & Team <ul><li>Started by Groovy enthusiasts: Guillaume LaForge, Steven Devijver and Graeme Rocher (current project lead) </li></ul><ul><li>0.1 was out March 29th, 2006 </li></ul><ul><li>Current Team: Graeme Rocher, Marc Palmer, Dierk Koenig, Steven Devijer, Jason Rudolph, Sven Haiges... YOU! </li></ul>
  8. 8. Using Grails <ul><li>Standalone </li></ul><ul><ul><li>Configuration details are hidden </li></ul></ul><ul><ul><li>0..100 in no time! </li></ul></ul><ul><li>Integrate with existing applications </li></ul><ul><ul><li>Still use the flexibility of Grails for existing DB-Schemas, HBM-Mappings, Spring Configuration, etc. </li></ul></ul><ul><ul><li>Pimp your app! </li></ul></ul>
  9. 9. Powerful Foundations <ul><li>Foundations </li></ul><ul><ul><li>Groovy: 1 st class integration with Java Platform and all Java Code you have ever written! </li></ul></ul><ul><ul><li>Spring: IoC, Spring MVC, WebFlow, ... </li></ul></ul><ul><ul><li>Hibernate: powerful p ersistence Layer </li></ul></ul><ul><ul><li>SiteMesh: flexible layout framework </li></ul></ul>
  10. 10. Installing Grails <ul><li>No previous Groovy installation needed, just a Java VM 1.4+ (but does not hurt :-) </li></ul><ul><li> </li></ul><ul><li>Download, extract, set GRAILS_HOME, add bin to PATH, run it! </li></ul><ul><li>Type „grails“ to verify it works </li></ul>
  11. 11. Your First Grails App <ul><li>„ grails create-app“ </li></ul><ul><li>„ cd appName” </li></ul><ul><li>„ grails run-app“ </li></ul><ul><li>„ grails create-domain-class“, edit class </li></ul><ul><li>„ grails generate-all“ </li></ul><ul><li>„ grails run-app“ </li></ul>
  12. 12. Grails Foundations
  13. 13. Spring <ul><li>Spring Framework is used to hold everything together </li></ul><ul><li>You can specify your own Spring Beans for injection into Controllers, Services, Jobs in spring/resources.xml </li></ul><ul><li>Good to know: a Grails app is an Spring MVC app... Spring Webflow to be used in future </li></ul>
  14. 14. Hibernate <ul><li>Hibernate is the de facto standard for O/R Mapping </li></ul><ul><li>Grails maps your Domain Classes automatically, even creates and exports the schema to the database </li></ul><ul><ul><li>1:n & m:n supported! </li></ul></ul><ul><li>Great flexibility: use your own HBM files for legacy database schemas! </li></ul>
  15. 15. SiteMesh <ul><li>SiteMesh is a powerful Layout Framework and integrated into Grails </li></ul><ul><li>grails-app/views/layout/main.gsp is the standard layout file </li></ul><ul><li>This meta element in a head of a gsp file links it with the “main” layout: < meta name = &quot;layout&quot; content = &quot;main&quot; /> </li></ul>
  16. 16. Grails Philosophy <ul><li>Reuse (see foundations) </li></ul><ul><li>No “reinventing the wheel” </li></ul><ul><li>Coding by Convention </li></ul><ul><ul><li>without being locked into a single solution! </li></ul></ul><ul><li>Domain-Centric, not DB-Centric </li></ul><ul><li>DRY </li></ul>
  17. 17. Grails MVC
  18. 18. Model <ul><li>Above is a Domain Class </li></ul><ul><ul><li>.groovy files living in grails-app/domain </li></ul></ul><ul><ul><li>Persistent </li></ul></ul><ul><ul><li>id, version, equals, hashCode, toString is generated automatically, but can be overridden </li></ul></ul><ul><li>class Book { </li></ul><ul><li>String title </li></ul><ul><li>} </li></ul>
  19. 19. Model <ul><li>You will typically use this convenience target to create Domain Classes </li></ul><ul><li>Will ask you for the name, make it uppercase if you forget </li></ul><ul><li>> grails create-domain-class </li></ul>
  20. 20. Model <ul><li>1:1 mapping with Author </li></ul><ul><li>Specify the “owning side” with this </li></ul><ul><li>class Book { </li></ul><ul><li>Author author </li></ul><ul><li>String title </li></ul><ul><li>} </li></ul><ul><li>class Book { </li></ul><ul><ul><li>def belongsTo = Author </li></ul></ul><ul><li>Author author </li></ul><ul><li>String title </li></ul><ul><li>} </li></ul>
  21. 21. Model <ul><li>1:n mapping: Author has many Books </li></ul><ul><li>Property “books” created automatically </li></ul><ul><li>Same for adder method: </li></ul><ul><li>class Author { </li></ul><ul><ul><li>def hasMany = [ books : Book ] </li></ul></ul><ul><li>String name </li></ul><ul><li>} </li></ul><ul><li>author.addBook(new Book(title:'Grails')) </li></ul><ul><li> </li></ul>
  22. 22. Model <ul><li>m:n possible, too! </li></ul><ul><li>BelongsTo defines “owning side” </li></ul><ul><li>class Book { </li></ul><ul><li>def belongsTo = Author </li></ul><ul><li>def hasMany = [authors:Author] </li></ul><ul><li>} </li></ul><ul><li>class Author { </li></ul><ul><li>def hasMany = [books:Book] </li></ul><ul><li>} </li></ul>
  23. 23. Model <ul><li>Constraints defined via constraints closure </li></ul><ul><li>Affects generation of views, too! </li></ul><ul><li>class User { </li></ul><ul><li>String login </li></ul><ul><li>String password </li></ul><ul><li>String email </li></ul><ul><li>Date age </li></ul><ul><li>static constraints = { </li></ul><ul><li>login(length:5..15,blank:false,unique:true) </li></ul><ul><li>password(length:5..15,blank:false) </li></ul><ul><li>email(email:true,blank:false) </li></ul><ul><li>age(min:new Date(),nullable:false) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 24. Model <ul><li>Dynamic Finder Methods </li></ul><ul><li>def results = Book.findByTitle(&quot;The Stand&quot;) </li></ul><ul><li>results = Book.findByTitleLike(&quot;Harry Pot%&quot;) </li></ul><ul><li>results = Book.findByReleaseDateBetween( firstDate, secondDate ) </li></ul><ul><li>results = Book.findByReleaseDateGreaterThan( someDate ) </li></ul><ul><li>You can also use Query by Example (QBE) the Hibernate Criteria Builder, or direct HQL queries! </li></ul>
  25. 25. Model <ul><li>Database configuration is done in grails-app/conf directory </li></ul><ul><ul><li>DevelopmentDataSource.groovy </li></ul></ul><ul><ul><li>ProductionDataSource.groovy </li></ul></ul><ul><ul><li>TestDataSource.groovy </li></ul></ul><ul><li>If you hit “grails run-app” the dev datasource is the default </li></ul>
  26. 26. Model <ul><li>“grails war” creates your deployment war file with the production datasource </li></ul><ul><li>class DevelopmentDataSource { </li></ul><ul><li>boolean pooling = true </li></ul><ul><li>// one of 'create', 'create-drop','update' </li></ul><ul><li>//String dbCreate = &quot;create-drop&quot; </li></ul><ul><li>String url = &quot;jdbc:postgresql://localhost:5432/act_dev&quot; </li></ul><ul><li>String driverClassName = &quot;org.postgresql.Driver&quot; </li></ul><ul><li>String username = &quot;dev&quot; </li></ul><ul><li>String password = &quot;devpw&quot; </li></ul><ul><li>def logSql = true //enable logging of SQL generated by Hibernate </li></ul><ul><li>} </li></ul>
  27. 27. Model <ul><li>BootStrap files are picked up at startup and can be used to create initial (test) data </li></ul><ul><li>class CompanyBootStrap { </li></ul><ul><li>def init = { servletContext -> </li></ul><ul><li>//Delete all data in the tables that we work on </li></ul><ul><li>Company.executeUpdate( &quot;delete Company&quot; ) </li></ul><ul><li>//now create the test data </li></ul><ul><li>Company c1 = new Company(name: 'bmw' ) </li></ul><ul><li>c1.addUser( new User(name: 'herbert' , admin: true , ...)) </li></ul><ul><li>.save() </li></ul><ul><li>} </li></ul><ul><li>... </li></ul>
  28. 28. View <ul><li>Groovy Server Pages(GSP) or JavaServer Pages (JSP) </li></ul><ul><li>TagLibs for both, but GSP is groovin' </li></ul><ul><li>All views are in the grails-app/views directory </li></ul><ul><li>Generate views for your domain class: “grails generate-views” </li></ul>
  29. 29. View <ul><li>Example GSP / uses “main” Layout </li></ul><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 > BlogEntry List </ title > </li></ul><ul><li></ head > </li></ul><ul><li>< body > </li></ul><ul><li>< div class = &quot;nav&quot; > </li></ul><ul><li>< span class = &quot;menuButton&quot; > < g:link controller = &quot;authentication&quot; action = &quot;viewControllers&quot; > Home </ g:link ></ span > </li></ul><ul><li>< span class = &quot;menuButton&quot; >< g:link action = &quot;create&quot; > New BlogEntry </ g:link ></ span > </li></ul><ul><li></ div > </li></ul><ul><li>< div class = &quot;body&quot; > </li></ul><ul><ul><li>... </li></ul></ul><ul><li></ div > </li></ul><ul><ul><li>... </li></ul></ul>
  30. 30. View <ul><li>Dynamic Tag Libraries </li></ul><ul><li>Tags are fun again! </li></ul><ul><li>“grails create-taglib” </li></ul><ul><li>A plain *TagLib.groovy file in grails-app/taglib </li></ul><ul><li>As with GSPs & Controllers: no server restart! </li></ul>
  31. 31. View <ul><li>TagLib example (“simple” tag) </li></ul><ul><li>class MyTagLib { </li></ul><ul><ul><li>includeJs = { attrs -> </li></ul></ul><ul><li>out << &quot;<script src='scripts/ ${attrs['script']} .js' />&quot; </li></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>OutputStream is available via “out” </li></ul><ul><li>All attributes are in the attrs map </li></ul><ul><li>Usage of GStrings keeps tags readable </li></ul>
  32. 32. View <ul><li>Logical and iterative tags just as easy. </li></ul><ul><li>You can even call tags as “methods” within GSP – here used as normal tag: </li></ul><ul><li><g:hasErrors bean=&quot;${book}&quot; field=&quot;title&quot;> </li></ul><ul><li><span class='label error'>There were errors on the book title</span> </li></ul><ul><li></g:hasErrors> </li></ul><ul><li>Or as method: </li></ul><ul><li><span id=&quot;title&quot; class=&quot;label ${hasErrors(bean:book,field:'title','errors')}&quot;>Title</span> </li></ul>
  33. 33. View <ul><li>Creating markup from tags is a piece of cake: </li></ul><ul><li>def dialog = { attrs, body -> </li></ul><ul><li>mkp { </li></ul><ul><li>div('class':'dialog') { </li></ul><ul><li>body() </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Above example uses a Groovy MarkupBuilder </li></ul>
  34. 34. Controller <ul><li>All controllers are mapped to the Grails dispatcher servlet </li></ul><ul><li>Create a new controller via: “grails create-controller” </li></ul><ul><li>Or generate the controller based on an existing Domain Class: “grails generate-controller” </li></ul>
  35. 35. Controller <ul><li>Default Mapping Convention http://.../appName/controller/action/id </li></ul><ul><li>Some properties automatically available </li></ul><ul><ul><li>flash – map stores messages for next request </li></ul></ul><ul><ul><li>log - a Log4J logger instance </li></ul></ul><ul><ul><li>params – map with all request parameters </li></ul></ul><ul><ul><li>request/response/servletContext etc. </li></ul></ul>
  36. 36. Controller <ul><li>Example generated controller: </li></ul><ul><li>class AdvertisementController { </li></ul><ul><li>def index = { redirect(action:list,params:params) } </li></ul><ul><li>def list = { </li></ul><ul><li>[ advertisementList: Advertisement.list( params ) ] </li></ul><ul><li>} </li></ul><ul><li>def show = { </li></ul><ul><li>[ advertisement : Advertisement.get( ) ] </li></ul><ul><li>} </li></ul><ul><li>... </li></ul>
  37. 37. Controller <ul><li>You can also use dynamic scaffolding: </li></ul><ul><li>class BookController { </li></ul><ul><li>def scaffold = Book </li></ul><ul><li>} </li></ul><ul><li>list/show/edit/delete/create/save/ update “dynamically” available </li></ul><ul><li>You can still override actions </li></ul><ul><li>“grails generate-all” creates all views and all controller actions for a domain class </li></ul>
  38. 38. Controller <ul><li>Grails Services can be used to encapsulate business logic </li></ul><ul><li>Services may be injected into Controllers </li></ul><ul><li>“grails create-service” creates a new service in grails-app/services </li></ul>
  39. 39. Controller <ul><li>Example Service </li></ul><ul><li>class CountryService { </li></ul><ul><li>def String sayHello(String name) { </li></ul><ul><li>return &quot;hello ${name}&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Used within a controller </li></ul><ul><li>class GreetingController { </li></ul><ul><li>CountryService countryService </li></ul><ul><li>def helloAction = { </li></ul><ul><li>render(countryService.sayHello( </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  40. 40. About Scaffolding... <ul><li>Use it to get up to speed, have something to show and work on </li></ul><ul><li>You will not scaffold your complete web application </li></ul><ul><li>Learn from the generated code, tweak it to fit your individual needs </li></ul>
  41. 41. Grails Features
  42. 42. Auto Reloading <ul><li>In development mode, Grails synchronizes the current application with the server </li></ul><ul><li>Controllers, GSPs, Tag Libraries, Domain Classes, Services, etc. </li></ul><ul><li>This is essential for an iterative and incremental development. </li></ul><ul><li>Call it “agile” if you like! </li></ul>
  43. 43. Spring Integration <ul><li>You can put additional Spring configuration in the /spring directory and use it to configure your beans </li></ul><ul><li>Automatic DI is available even for these beans (not just Services) </li></ul><ul><li>Easily integrate existing (Java) functionality that was configured with Spring </li></ul>
  44. 44. Hibernate Integration <ul><li>You can specify your own HBM files for your domain classes. (You can even use your existing Java classes if you like) </li></ul><ul><li>Working with legacy database schemas is getting really simple </li></ul><ul><li>Still use all benefits like dynamic finder methods </li></ul>
  45. 45. Eclipse “Integration” <ul><li>Grails creates an Eclipse Project file automatically, just run File > Import > Existing Project </li></ul><ul><li>You'll have to tweak some file names if you use the development snapshot versions </li></ul><ul><li>Be sure to install the Groovy Plugin: </li></ul>
  46. 46. AJAX <ul><li>Grails supports AJAX with different AJAX toolkits, currently Prototype, Dojo and Yahoo </li></ul><ul><li>Special AJAX tags can be used for asynchronous calls and form submission </li></ul><ul><li><div id=&quot;message&quot;></div> </li></ul><ul><li><g:remoteLink action=&quot;delete&quot; id=&quot;1&quot; update=&quot;message&quot;>Delete Book</g:remoteLink> </li></ul>
  47. 47. AJAX <ul><li>On the server side, Grails supports AJAX via the render() Method, which makes AJAX responses really easy: </li></ul><ul><li>def time = { </li></ul><ul><li>render(contentType:'text/xml') { </li></ul><ul><li>time(new Date()) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Render() supports MarkupBuilders for XML, HTML, JSON, OpenRico </li></ul>
  48. 48. Job Scheduling <ul><li>Grails makes using Quartz even easier, just create this (in grails-app/jobs): </li></ul><ul><li>class MyJob { </li></ul><ul><ul><li>def cronExpression = &quot;0,15,30,45 * * * * ?&quot; //every 15 seconds </li></ul></ul><ul><ul><li>def execute(){ </li></ul></ul><ul><ul><ul><li>println &quot;Running job!&quot; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  49. 49. Unit & Functional Testing <ul><li>Both unit and functional testing supported </li></ul><ul><li>Functional Testing uses Canoo Webtest </li></ul><ul><ul><li>“grails test-app” runs the unit tests </li></ul></ul><ul><ul><li>“grails run-webtest” </li></ul></ul><ul><li>Generate a webtest with </li></ul><ul><ul><li>“grails generate-webtest” </li></ul></ul>
  50. 50. Grails Roadmap
  51. 51. The Sandbox <ul><li>New Controllers </li></ul><ul><li>Page Flows </li></ul><ul><li>Constraints for DB-Schema creation </li></ul><ul><li>Laszlo on Grails </li></ul><ul><li>Mail / Messaging Integration </li></ul><ul><li>Test DataSets </li></ul><ul><li>Plugin-System </li></ul>
  52. 52. Roadmap <ul><li>0.3 </li></ul><ul><ul><li>Spring 2 </li></ul></ul><ul><ul><li>Web Services Service Classes </li></ul></ul><ul><ul><li>M:N Mapping for GORM </li></ul></ul><ul><li>0.4 </li></ul><ul><ul><li>DWR Support for Service Classes </li></ul></ul><ul><ul><li>Pluggable Persistence Layer </li></ul></ul>
  53. 53. Roadmap <ul><li>0.5 </li></ul><ul><ul><li>XML-RPC for Service Classes </li></ul></ul><ul><ul><li>Generation of Domain Model from DB Schema </li></ul></ul><ul><li>0.6 </li></ul><ul><ul><li>Scaffolding of User Authentication Code </li></ul></ul><ul><ul><li>Grails is user-centric, you define what really happens. Vote in Jira: </li></ul></ul>
  54. 54. Your Next Steps
  55. 55. Learn more <ul><li>Download, Install, create your first app with the Quickstart Guide </li></ul><ul><li>Check out the Tutorials Section </li></ul>
  56. 56. Learn more <ul><li>Read & Work through the user guide </li></ul><ul><li>Get onto the Grails user mailing list </li></ul><ul><li>Remember: Grails is open source... </li></ul><ul><ul><li>The more you contribute, the more you get out in terms of learning, jobs, contacts... </li></ul></ul>
  57. 57. Watch <ul><li>Currently two screencasts available </li></ul><ul><ul><li>Scaffolding </li></ul></ul><ul><ul><li>Directory Structure </li></ul></ul><ul><ul><li> </li></ul></ul>
  58. 58. Listen <ul><li>Grails Podcast </li></ul><ul><li>Weekly Show </li></ul><ul><li>Grails News </li></ul><ul><li>Grails Features </li></ul><ul><li>Interviews </li></ul><ul><li>Agile Development </li></ul><ul><ul><li> </li></ul></ul>
  59. 59. Read <ul><li>Out soon! </li></ul><ul><li>By Graeme Rocher, Grails Project Lead </li></ul><ul><li>ISBN: 1-59059-758-3 </li></ul>
  60. 60. Contribute <ul><li>Be part of the community </li></ul><ul><li>Contribute a Tag! </li></ul><ul><li>Blog about Grails! Now! </li></ul><ul><li>Answer questions on the Grails Users List! </li></ul><ul><li>and... </li></ul>
  61. 61. Groovy & Grails Shop <ul><li>Buy your girlfriend one of those sexy Groovy GStrings </li></ul><ul><li> </li></ul>New!
  62. 62. GRAILS <ul><ul><li>RAPID WEB APP DEVELOPMENT MADE EASY </li></ul></ul><ul><ul><li>Questions? </li></ul></ul><ul><ul><li> </li></ul></ul>