Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
2007 09 10 Fzi Training Groovy Grails V Ws
1.
2.
3. Why should I care ? 40-60% User Interface 40-60% Functionality Software Development Effort Variance Max. 20 % User Interface Min. 80% Functionality Focus : Users Great Interface Developer Gigantic Functionality Typically Research … try to be efficient in programming efforts … ... try to be efficient in UI efforts...
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33. GRAILS // Layering on Java Components Source: G. K. Rocher, The Definite Guide to Grails, Apress, 2006, p. 7
39. GRAILS // Step 3 Generation of views and controllers • Once the Book class is created, let´s benefint from static scaffolding -> grails generate-all -> grails run-app • A BookController is created, as well as four GSP: ▪ create.gsp ▪ edit.gsp ▪ list.gsp ▪ show.gsp
41. GRAILS // Taking control of GRAILS V iew M odel C ontroller grails generate-controller /grails-app/controllers grails generate-views /grails-app/views grails create-domain-class /grails-app/model J obs S ervices /grails-app/jobs /grails-app/services grails create-app 1 2 Web Test Unit Test 4 5 6 grails generate-all 3
42. GRAILS // VIEW CONTROLLER INTERACTION Source: G. K. Rocher, The Definite Guide to Grails, Apress, 2006
43. GRAILS // Hibernate interaction • Hibernate is the de facto ORM solution • Domain classes are automatically and transparently mapped with Hibernate • 1:1, 1:n & m:n realtionships supported • Various database environments (dev, test, prod) • Default HSQLDB in-memory config • You can even provide your own Hibernate mapping files for legacy schemas • You can also reuse your EJB3 ! V M C S J
44. GRAILS // A more complex domain class Author { String firstName String lastName def hasMany = [books: Book ] String toString() { “ $firstName $lastName” } } class Book { String title Author author Publisher publisher def belongsTo = [ Publisher , Author ] String toString() { title } } class Publisher { String name def hasMany = [ books: Book ] String toString() { name } } Author has many Books Publisher has many Books V M C S J
45. GRAILS // Domain with constraints to keep valid Add an email to Author class Author { String email // … static constraints = { email (email: true) } } Add an ISBN to Book class Book { Stringisbn // … static constraints = { isbn (matches: “ [0-9] {9} [0-9X ]”) } } V M C S J
46. GRAILS // ... and even more constraints • Many constraints available: blank , creditcard, email, inList , length, min, minLength, minSize, matches , max, maxLength, maxSize, notEqual, nullable , range , size, unique , url , validator • And you can create your own closure validator: even( validator: { it % 2 == 0 )} V M C S J
47.
48. GRAILS // Query Examples • Book. find By Title (“The Stand”) Book.findByTitle Like (“Harry Pot%”) Book.findByReleaseData Between (start, end) Book.findByTitleLike Or ReleaseData LessThan ( “ %Grails%”, someData) • Find by relationship Book.find All By Author ( Author.get(1) ) • Affect sorting Book.findAllbyAuthor(me, [sort:‘title’,order:’asc’] ) V M C S J
49. GRAILS // Views • Spring MVC under the hood • Support for flash scope between requests • GSP : Groovy alternative to JSP • Dynamic taglib development: no TLD, no configuration, just conventions • Adaptive AJAX tags (Yahoo, Dojo, Prototype) • Customizable layout with SiteMesh • Page fragments through reusable templates • View under grails-app/views V M C S J TIP: Use grails install-templates to install and customize your the templates to be used for generation (templates in ../src/templates)
50. GRAILS // GSP Groovy Server Pages <html> <head> <meta name=“layout“ content=“main“ /> <title>Book List</title> </head> <body> <a href=“ $ {createLinkTo(dir:’’)} ”>Home</a> <g:link action=“create”> New Book</g:link> <g:if test=“ $ { flash.message } ”> ${flash.message} </g:if> <g:each in=“${bookList}”> $ {it.title} </g:each> </body> </html> V M C S J
51. GRAILS // Rich Set of Dynamic Tag Libs • Logical: if, else, elseif • Iterative: while, each, collect, findAll… • Linking: link, createLink, createLinkTo • Ajax: remoteFunction, remoteLink, formRemote, submitToRemote… • Form: form, select, currencySelect, localSelect, datePicker, checkBox… • Rendering: render*, layout*, paginate… • Validation: eachError, hasError, message • UI: rich TextEditor… V M C S J
52. GRAILS // URL Convention • URL mapping convention: controller/action/id http://localhost:8080/library/ book / show / 1 • Scaffolding can be ▪ dynamic (def scaffold = true) ▪ static (code generation) • Controllers pass data to the view through maps • Direct access to parameters • Easy redirect and forward • Can define allowed methods for each action V M C S J
53. GRAILS // Controllers provide actions to execute (call) class Book Controller { def index = { redirect(action:list,params:params) } def list = { [ bookList: Book.list( params )] } def show = { [ book : Book.get( params.id ) ] } def edit = { def book = Book.get( params.id ) if(!book) { flash.message = “Book ${params.id} not found” redirect(action:list) } else return [ book : book ] } } V M C S J
54. GRAILS // Chaining and Flashing Source: G. K. Rocher, The Definite Guide to Grails, Apress, 2006
55. GRAILS // Services • Services are Groovy classes that should contain your business logic • Automatic injection of services in controllers & services simply by declaring a field: class BookController { MySuperService mySuperService } V M C S J
56. Grails // Jobs • You can create recuring events with Quartz under the hood, configured by Spring • Again a convention on the name and directory • Regular intervals, or cron definitions • class My Job {} def cronExpression = “0 0 24 * * ?” def execute() { print “Job run!” } } V M C S J