Groovygrailsnetbeans 12517452668498-phpapp03

Embed Size (px)

Citation preview

  • 1. Agile Web Development withGroovy and Grails Carol McDonald, Java Architect

2. ObjectiveOverview of theGrails Web Platform2 3. Groovy Overview3 4. What is Groovy? A dynamic language written for the JVM> Generates byte code Java like syntax> Easy learning curve for Java developers Seamless integration with Java> A Groovy object is a Java Object> Groovy application can create Java objects> Java objects can create Groovy objects 4 5. A Valid Java Programimport java.util.*;public class Erase {private List filterLongerThan(List strings, int length) { List result = new ArrayList();for (String n: strings) if (n.length() A return to POJO and annotations in JPA but still lotsof stuff to learn Numerous layers and configuration files lead to chaos> Adoption of frameworks a good thing, but too oftenlead to configuration file hell Ugly JSPs with scriptlets and complexity of JSPcustom tags Grails addresses these without compromisingtheir benefits 10 11. Grails Technology Stack11 12. How To Get Started Download Grails> http://grails.org Configure Netbeans 6.5 Download Groovy> http://groovy.codehaus.org Set GROOVY_HOME, GRAILS_HOMEFor command Add bin to your PATHline> $GROOVY_HOME/bin:$GRAILS_HOME/bin12 13. Using Grails 13 14. grailscreateapp Will be prompted for the name of the application IDE runs the "grails create-app" command Generate a directory structure for> Grails source> Additional libraries> Configurations> web-app 14 15. Grails DirectoryStructureApplicationname Grail sourceAdditionalJARsWebapplication15 16. Netbeans GrailsProject StructureProject nameGrail sourceAdditionalJARsWebapplication 16 17. ConfigureforMySQL copy the mysql-connector-java-5.1.6-bin.jar to the lib directory Edit DataSource.groovydataSource {pooled = truedriverClassName = "com.mysql.jdbc.Driver"username = "root"password = ""dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"}environments {development { dataSource { dbCreate = "update" // one of create, create-drop,update url = "jdbc:mysql://localhost/petcatalog" }}17 18. MVC and Grails Models, or domainclasses, represent theproblem domain 18 19. grailscreatedomainclass The Model is your applications persistent business domain objects. 19 20. add the domain class attributesclass Item {Long idString nameString descriptionString imageurlString imagethumburlBigDecimal price}Groovy with Grails dynamically generates getters and settersand the dynamic methods Item.save(), Item.delete(), Item.list(), Item.get() to retrieve/update data from/to the db table. 20 21. Persistence Methods GORM automatically provides persistencemethods to your object> save(), get(), delete() Automatically generates addTo*() for objectrelationships> Where * is the name of the propertydefitem=newItem(name:Fred)deforder= newOrder(orderDate:newDate(),item:PSP)item.addToOrders(order)item.save() 21 22. Queries Dynamically generated queries> list, findBy Manual queries (not discussed here)> HibernateQL Examples> Item.list()> Item.findByName(nicecat)22 23. grailsdomainclassItem Address String name1 M String street String descriptionString zip Address addressstatic hasMany =[item:Item] Define relation between objects with attributes> hasMany, belongsTo> statichasMany=[item:Item]Groovy hashmap 23 24. Scaffolding Generates CRUD actions and views for thecorresponding domain class Dynamic:> Enable the scaffold property in the controller class defscaffold=true Static> grailsgenerateall> Generatesacontrollerandviewsforadomainclass Usefultogetupandrunningquickly 24 25. Scaffolding Generates a controller and views for the domain class25 26. MVC and Grails Controllers controlrequest flow, interactwith models, anddelegate to views. 26 27. grailscreatecontroller Controller made up of action methods> Grails routes requests to the action corresponding toURL mapping> Default action is indexclassItemController{ defindex={redirect(action:list,params:params)} deflist={http://host/catalog/item/listif(!params.max)params.max=10 [itemList:Item.list(params)] } defshow={...Actions27 28. URL Mappings Default mapping from URL to action method http://host/catalog/item/listApplication Controller Action Defined in> grailsapp/conf/UrlMappings.groovystaticmappings={"/$controller/$action?/$id?"28 29. grailscontrollerclassItemController{ defindex={redirect(action:list,params:params)} deflist={if(!params.max)params.max=10 [itemInstanceList:Item.list(params)] } defshow={...returns an ArrayList of item objectsretrieved from the item database tableitemInstanceList variable is madeavailable to the view29 30. MVC and Grails Views are defined in Groovy Server Pages (GSP) and render the model 30 31. grailsview actions usually render a Groovy Server Page in the views directory corresponding to the name of the controller and action. list.gspg:eachGroovy Tag loops through each object in the itemInstanceList variable${fieldValue(bean:itemInstance,field:price)} displays the value of the item s price attribute31 32. grailsrunapp Will start the embedded web server and run theapplication Defaults to> http://localhost:8080/ 32 33. Summary Groovy is a powerful dynamic language for theJVM Grails is a full featured web platform33 34. http://weblogs.java.net/blog/caroljmcdonald /34 35. Acknowldegement Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides 35 36. Carol McDonaldJava Architect Tech Days 200936 37. Lots of Books and Online Tutorials 37