• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Agile web development Groovy Grails with Netbeans
 

Agile web development Groovy Grails with Netbeans

on

  • 5,068 views

 

Statistics

Views

Total Views
5,068
Views on SlideShare
4,980
Embed Views
88

Actions

Likes
0
Downloads
33
Comments
0

5 Embeds 88

http://owen.com 73
http://localhost 7
http://www.slideshare.net 6
http://zootool.com 1
http://sysdecom-projects.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Agile web development Groovy Grails with Netbeans Agile web development Groovy Grails with Netbeans Presentation Transcript

    • Agile Web Development with Groovy and Grails Carol McDonald, Java Architect
    • Objective Overview of the Grails Web Platform
    • Groovy Overview
    • 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
    • A Valid Java Program
      • import java.util.*;
      • public class Erase {
        • private List<String> filterLongerThan(List<String> strings, int length) {
          • List<String> result = new ArrayList<String>();
          • for (String n: strings)
            • if (n.length() <= length)
              • result.add(n);
          • return (result);
        • }
        • public static void main(String[] args) {
          • List<String> names = new ArrayList<String>();
          • names.add(&quot;Ted&quot;);
          • names.add(&quot;Fred&quot;);
          • names.add(&quot;Jed&quot;);
          • names.add(&quot;Ned&quot;);
          • System.out.println(names);
          • Erase e = new Erase();
          • List shortNames = e.filterLongerThan(names, 3);
          • System.out.println(shortNames);
        • }
      • }
    • A Valid Groovy Program
      • import java.util.*;
      • public class Erase {
        • private List<String> filterLongerThan(List<String> strings, int length) {
          • List<String> result = new ArrayList<String>();
          • for (String n: strings)
            • if (n.length() <= length)
              • result.add(n);
          • return (result);
        • }
        • public static void main(String[] args) {
          • List<String> names = new ArrayList<String>();
          • names.add(&quot;Ted&quot;);
          • names.add(&quot;Fred&quot;);
          • names.add(&quot;Jed&quot;);
          • names.add(&quot;Ned&quot;);
          • System.out.println(names);
          • Erase e = new Erase();
          • List shortNames = e.filterLongerThan(names, 3);
          • System.out.println(shortNames);
        • }
      • }
    • The Groovy Way
        • def names = [&quot;Ted&quot;, &quot;Fred&quot;, &quot;Jed&quot;, &quot;Ned&quot;]
        • println names
        • def shortNames = names.findAll{
        • it.size() <= 3
        • }
        • print shortNames
    • Grails Overview
    • What is Grails?
      • An Open Source Groovy MVC framework for web applications
      • Principles
        • CoC – Convention over configuration
        • DRY – Don't repeat yourself
      • Similar to RoR but with tighter integration to the Java platform
    • Why Grails?
      • ORM layer can be overly difficult to master and get right
        • A return to POJO and annotations in JPA but still lots of stuff to learn
      • Numerous layers and configuration files lead to chaos
        • Adoption of frameworks a good thing, but too often lead to configuration file hell
      • Ugly JSPs with scriptlets and complexity of JSP custom tags
      • Grails addresses these without compromising their benefits
    • Grails Technology Stack
    • How To Get Started
      • Download Grails
        • http://grails.org
      • Configure Netbeans 6.5
      • Download Groovy
        • http://groovy.codehaus.org
      • Set GROOVY_HOME , GRAILS_HOME
      • Add bin to your PATH
        • $GROOVY_HOME/bin:$GRAILS_HOME/bin
      For command line
    • Using Grails
    • grails create-app
      • Will be prompted for the name of the application
      • Generate a directory structure for
        • Grails source
        • Additional libraries
        • Configurations
        • web-app
      IDE runs the &quot;grails create-app&quot; command Demo
    • Grails Directory Structure Application name Additional JARs Web application Grail source
    • Netbeans Grails Project Structure Project name Grail source Additional JARs Web application
    • Configure for MySQL
      • copy the mysql-connector-java-5.1.6-bin.jar to the lib directory
      • Edit DataSource.groovy
      • dataSource {
      • pooled = true
      • driverClassName = &quot;com.mysql.jdbc.Driver&quot;
      • username = &quot;root&quot;
      • password = &quot;&quot;
      • dialect = &quot;org.hibernate.dialect.MySQL5InnoDBDialect&quot;
      • }
      • environments {
      • development {
      • dataSource {
      • dbCreate = &quot;update&quot; // one of 'create', 'create-drop','update'
      • url = &quot;jdbc:mysql://localhost/petcatalog&quot;
      • }
      • }
    • MVC and Grails
      • Models, or domain classes, represent the problem domain
    • grails create-domain-class
      • The Model is your application's persistent business domain objects.
    • add the domain class attributes
        • class Item {
        • Long id
        • String name
        • String description
        • String imageurl
        • String imagethumburl
        • BigDecimal price
        • }
      Groovy with Grails dynamically generates getters and setters and the dynamic methods Item.save(), Item.delete(), Item.list(), Item.get() to retrieve/update data from/to the db table.
    • Persistence Methods
      • GORM automatically provides persistence methods to your object
        • save() , get() , delete()
      • Automatically generates addTo * () for object relationships
        • Where * is the name of the property
      def item = new Item(name:'Fred') def order = new Order(orderDate: new Date(), item: 'PSP') item. addToOrders (order) item. save ()
    • Queries
      • Dynamically generated queries
        • list , findBy
      • Manual queries (not discussed here)
        • HibernateQL
      • Examples
        • Item.list()
        • Item.findBy Name ('nice cat')
    • grails domain class
      • Define relation between objects with attributes
        • hasMany , belongsTo
        • static hasMany = [ item: Item ]
      Groovy hashmap Address String street String zip static hasMany = [item:Item] 1 M Item String name String description Address address
    • Scaffolding
      • Generates CRUD actions and views for the corresponding domain class
      • Dynamic:
        • Enable the scaffold property in the controller class
          • def scaffold = true
      • Static
        • grails generate-all
        • Generates a controller and views for a domain class
      • Useful to get up and running quickly
    • Scaffolding
      • Generates a controller and views for the domain class
      Demo
    • MVC and Grails
      • Controllers control request flow, interact with models, and delegate to views.
    • grails create-controller
      • Controller made up of action methods
        • Grails routes requests to the action corresponding to URL mapping
        • Default action is index
      class Item Controller { def index = { redirect(action:list,params:params) } def list = { if(!params.max) params.max = 10 [ itemList: Item.list( params ) ] } def show = {... Actions http://host/catalog/item/list
    • URL Mappings
      • Default mapping from URL to action method
          • http://host/catalog/item/list
      • Defined in
        • grails-app/conf/UrlMappings.groovy
      static mappings = { &quot;/$controller/$action ? /$id ? &quot; Application Controller Action
    • grails controller class Item Controller { def index = { redirect(action:list,params:params) } def list = { if(!params.max) params.max = 10 [ itemInstanceList: Item.list ( params ) ] } def show = {... returns an ArrayList of item objects retrieved from the item database table itemInstanceList variable is made available to the view
    • MVC and Grails
      • Views are defined in Groovy Server Pages (GSP) and render the model
    • grails view
      • actions usually render a Groovy Server Page in the views directory corresponding to the name of the controller and action. list.gsp
      <html> <table> <tbody> < g:each in=&quot;${ itemInstanceList }&quot; status=&quot;i&quot; var=&quot; itemInstance &quot;> <td> ${fieldValue (bean: itemInstance , field:' price ')}</td> </tr> </g:each> </tbody> </table> g:each Groovy Tag loops through each object in the itemInstanceList variable displays the value of the item 's price attribute
    • grails run-app
      • Will start the embedded web server and run the application
      • Defaults to
        • http://localhost:8080/<app_name>
      Demo
    • Summary
      • Groovy is a powerful dynamic language for the JVM
      • Grails is a full featured web platform
    • http://weblogs.java.net/blog/caroljmcdonald/
    • Acknowldegement
      • Thanks to Guilaume Laforge of G2One for allowing me to use and adapt some of his slides
      • Presenter’s Name
        • [email_address]
      Carol McDonald Java Architect Tech Days 2009
    • Lots of Books and Online Tutorials