Java Users Group - PowerPoint PPT Presentation

About This Presentation
Title:

Java Users Group

Description:

Uses ANTLR to compile Groovy down to JVM compatible bytecode. Predicated ... AJAX and DHTML. Dojo. Prototype. Yahoo!. Installation. Visit http://www.grails.org ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 34
Provided by: jasonmc3
Category:
Tags: dhtml | group | java | users

less

Transcript and Presenter's Notes

Title: Java Users Group


1
Java Users Group
Introduction to Grails
by Jason McDonald
  • Charleston, SC

June 25, 2008
2
Getting Started
3
Grails Basics
  • Open Source
  • Fully integrated with Java
  • Groovy based
  • Uses ANTLR to compile Groovy down to JVM
    compatible bytecode
  • Predicated on DRY principle

4
Underlying Technologies
  • Spring
  • Hibernate
  • Log4J
  • Junit
  • Quartz
  • Ant

5
Native Support
  • HSQL
  • MySQL
  • Servlet container servers
  • Jetty
  • Tomcat
  • AJAX and DHTML
  • Dojo
  • Prototype
  • Yahoo!

6
Installation
  • Visit http//www.grails.org
  • Download and extract files
  • Create GRAILS_HOME environment variable
  • Add GRAILS_HOME to PATH environment variable
  • Thats it!

7
Grails Structure
8
Goovy Roots
  • Built on top of Java
  • Very similar syntax to java
  • Supports closures
  • Method returns can be slightly different
  • Case sensitive
  • No semi-colons needed
  • All classes end in .groovy

9
Conditional Environments
  • Environment conditionals allow for variations for
    a specific environment
  • Database
  • Initialization options
  • More
  • Three environment choices
  • Development
  • Testing
  • Production

10
Script Based Code Generation
  • grails create-app app name
  • grails create-controller controller name
  • grails create-domain-class class name
  • grails create-service service name
  • grails create-unit-test test name
  • grails create-tag-lib taglib name
  • grails generate-all class name
  • grails generate-views class name

11
Building, Deploying, and Managing
  • grails clean
  • grails compile
  • grails console
  • grails doc
  • grails install-plugin
  • grails run-app
  • grails war

12
Important Files
  • conf/DataSource.groovy
  • Database connections
  • conf/UrlMapping.groovy
  • Routing
  • conf/BootStrap.groovy
  • Bootstrap file
  • conf/Config.groovy
  • Configurations (MIME mappings, more)

13
Routing
  • Route rules found in conf/UrlMapping.groovy
  • Default route
  • application/controller/action/id?params
  • Additional rules and restrictions can be applied
    herestatic mappings "/product/id?"(contro
    ller"product") action GET"show",
    PUT"update", DELETE"delete", POST"save"

14
Databases
  • db configuration is in conf/DataSource.groovy
  • Can define at environment level or global level
  • Defaults to HSQL
  • Change to MySQL by
  • Dropping MySQL connector in the lib
  • Changing DataSource.groovy to point at MySQL

15
Database Create Scheme
  • dbCreate
  • create-drop creates tables on startup and drops
    them on shutdown (DEV)
  • create creates tables on startup, deletes data
    on shutdown (DEV, TEST)
  • update creates tables on startup, saves data
    between restarts (TEST, PROD)

16
Grails MVC
17
MVC Framework
  • Standard MVC implementation
  • Sits on top of Spring MVC
  • Reduces repetition of XML developers must
    maintain
  • Gives access to Spring DSL

18
Views
  • Groovy Server Pages
  • GSP extension
  • Based on JSP pages
  • Use
  • Tag libraries
  • Expressions
  • similar to JSP EL but allows any expression
    within

19
Tag Libraries
  • Ordering and sortingltgsortableColumn
    property"releaseDate"
    defaultOrder"desc" title"Release
    Date" titleKey"book.releaseDate" /gt
  • Form displayltgform name"myForm"
    action"myaction" id"1"gt ltgpasswordField
    name"myPasswordField value"myPassword"
    /gt ...lt/gformgt
  • FormattingltgformatDate format"yyyy-MM-dd"
    date"date"/gt

20
Custom Tag Libraries
  • Custom tag libraries are just groovy classes that
    end with TagLib
  • Defaults to g namespaces unless declared
  • Closure defines actual tagclass
    FormattingTagLib static namespace fmt
    def dateFormat attrs -gt out ltlt new
    java.text.SimpleDateFormat( dd-MM-yyyy).forma
    t(attrs.date) ltfmtdateFormat
    dateplan.effDate /gt

21
Internationalization Support
  • Based on Java i18n specifications
  • Define properties files with language specific
    entries
  • Views can read i18n entries with a
    tagltgmessage codemy.message argsOne,
    Two/gt
  • Tag libraries can read entries with
    codeg.message(code my.message, args One,
    Two)

22
Models
  • POGOs
  • Fields default to private
  • Getters and setters provided automatically
  • Must use wrapper objects no primitives (1.5?)
  • Field validation defined withinstatic
    constraints date(nullable false)
  • Can define custom messages using convention
  • className.propertyName.constraint Err msg
  • Automatic parameter mapping from
    viewbook.properties params

23
GORM
  • Grails Object Relational Mapping
  • Uses Hibernate3
  • Dynamic methods for findingBook.findByTitle(The
    Dark Tower)Book.findByTitleAndAuthor(The Dark
    Tower, Stephen KingBook.findByTitle(Christine
    , sort edition, order asc )
  • Relational mapping defined within
  • One to manystatic hasMany
    attendancesAttendance
  • One to oneAttendance attendance

24
GORM Caching
  • Provides the same caching mechanisms that can be
    found with Hibernate3
  • Updates to DataSource.groovy can toggle
    cachinghibernate cache.use_second_level_c
    ache true cache.use_query_cache true
    cache.provider_class
    org.hibernate.cache.EhCacheProvider

25
Controllers
  • House actions (methods) that respond to a request
  • Actions should map to GSP of same name
  • index is the default action
  • Can define the method each action is alloweddef
    allowedMethods save'POST', update'POST'
  • GET is the default
  • Auto scaffolding sets up CRUD without filesdef
    scaffold true // In Controller class

26
Action Responses
  • All actions respond in one of three ways
  • redirect
  • Equivalent ot response.sendRedirect
  • Can specify actions, controller, params, and more
  • return
  • Returns a value and calls a GSP of the same
    name(action method go will forward to go.gsp)
  • render
  • Calls a GSP by name
  • Has the ability to pass arguments

27
Grails in Action
28
Web Services
  • REST
  • Provide RESTful mappings in UrlMappings.groovy
  • Implement controller to accept and return
    marshalled data
  • SOAP
  • Supported through XFire plug-in
  • Simply expose a servicestatic expose xfire

29
Testing
  • Test framework based on JUnit
  • Unit tests
  • By convention uses mock objects
  • Integration tests
  • By convention use real objects
  • Functional tests
  • Supports Canoo WebTest integration

30
Running the Application
  • Bootstrapping data
  • Allows for initialization and test data
  • Jetty Server
  • Specify port and environment on command
    linegrails dev Dserver.port9999 run-app
  • Tomcat
  • As war
  • Exploded

31
The Example
32
Requirements
  • Application needs to
  • Store meetings, people, and which meetings each
    person attends
  • People should store first and last name
  • Meetings should store date and subject of meeting
  • Must have full CRUD capability
  • Must be web based
  • Must be able to integrate with Java environments
    (to meet future integration needs)

33
Open Discussion
Write a Comment
User Comments (0)
About PowerShow.com