def speaker = new Cast(name:"T.Yamamoto",version:"G*-2010-04-24")




                                                                1
2
3
4
5
6
7
8
9
10
View
            MVC2




       DI


11
12
ur ity
                                             S ec




                     low
    Controller
                              i1 8n
                                                              Flex




                      bF
                   We
                 Codec                   Quartz
    Domain

Grails                                      lug
                                               in
                                                              JMS
                                         MyP
     Core                  Quartz

                                                     ag gin
               Service          UrlMapping          T
     Filters
                                                                     13
14
15
16
17
18
19
20
% export GRAILS_HOME=/opt/grails-1.2.2
% export PATH=$GRAILS_HOME/bin:$PATH

% grails
Welcome to Grails 1.2.2 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /opt/grails-1.2.2
                       21
% grails run-app
% grails test-app




                    22
23
|--   application.properties
|--   grails-app
|     |-- conf        -
|     |-- controllers -
|     |-- domain      -
|     |-- i18n
|     |-- services    -
|     |-- taglib      -
|     |-- utils
|     `-- views       -
|--   lib
|--   scripts         -
|--   src             -
|--   test            -
`--   web-app         -
                           24
25
package com.pack

     class Book {

       String title
       String author
       int    rate
       String comment
       
       Date dateCreated
       Date lastUpdated
       
       static constraints = {
       }
     }
26
  static constraints = {
         title nullable:false,blank:false
         author nullable:false,blank:false
         rate range:0..10
         comment maxSize:1000
         price shared:'myShared'
         withTax display:false
       }

     grails.gorm.default.constraints = {
         '*'(nullable:true, size:1..20)
         myShared(nullable:true)
     }
27
class Book implements Serializable{
  static mapping = {
    table 'T_BOOK'
    id generator:'uuid.hex', params:[type:'string']
    columns {
      comment column:'F_COMMENT',sqlType:'varchar(1000)'
    }
    withTax formula: 'price * 1.05'
  }
                           28
29
Account.executeQuery(
    "select distinct a.number from Account a where a.branch = :branch",
    [branch:'London'],
    [max:10, offset:5]
)
Book.findAll("from Book as b where b.author=?",['               '])

                                   30
def results = Book.findAllByTitle("           ",
    [max:10, sort:"title", order:"desc", offset:100] )

def results = Book.findAllByTitleAndAuthor(
                        "             ", "         ")




                                                         31
def c = Account.createCriteria()
def results = c.list {  
    like("holderFirstName", "Fred%")  
    and {   
        between("balance", 500, 1000)   
        eq("branch", "London")  
    }  
    maxResults(10)
    order("holderLastName", "desc")
}

                    32
dataSource {
  pooled = true
  driverClassName = "org.hsqldb.jdbcDriver"
  username = "sa"
  password = ""
}

hibernate {
  cache.use_second_level_cache=true
  cache.use_query_cache=true
  cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
}

environments {
  development {
    dataSource {
      dbCreate = "update"
      url = "jdbc:hsqldb:file:devDB;shutdown=true"
    }
  }
  test {
    dataSource {
....   ....
                                       33
import javax.persistence.*
      
      @Entity
      @Table(name = "animal")
      class Animal {
      
        @Id 
        @GeneratedValue
        int id
        String name
        @ManyToOne
        @JoinColumn
        Owner owner
      
        static constraints = {
          name blank:false
        }
      }

34
35
http://localhost:8080/myapp/book/index

package com.pack
//
class BookController {
    //
    def index = {
        //
        render text:"Hello"
    }
}
             36
params
           servletContext,session,request,response
                beforeInterceptor afterInterceptor
def create = {
    def bookInstance = new Book()
    bookInstance.properties = params
    [bookInstance: bookInstance]
}

def save = {
    def bookInstance = new Book(params)
    if (bookInstance.save(flush: true)) {
        flash.message = "                 "
        redirect(action: "show", id: bookInstance.id)
    }
    else {
        render(view: "create", model: [bookInstance: bookInstance])
    }
}
                                                                      37
class BookController {


    def scaffold = true
}




              38
39
40
class SimpleWikiTagLib {
  static namespace = "wiki"
  static returnObjectForTags = ['content'] 
  def tagname = {attrs, body ->
    out<<"Hello"
  }
  
  def content = {attrs, body ->
    CmsContent.findByCode(attrs.code)?.content
  }
}



                            41
class UrlMappings {
  static mappings = {
    "/$controller/$action?/$id?"{
      constraints { }
    }
    "/"(view:"/index")
    "500"(view:'/error')
    //     URL      /blogname/2007/01/10/my_funky_blog_entry
    "/$blog/$year?/$month?/$day?/$id?"(
                controller:"blog", action:"show")
    //
      "/$blog/$year?/$month?/$day?/$id?" {
          controller = "blog"
          action = "show"
          constraints {
              year(matches:/d{4}/)
              month(matches:/d{2}/)     //
              day(matches:/d{2}/)       "/images/**.jpg"(controller:"image")
          }                           
      }
                                       42
name productDetail:"/showProduct/$productName/$flavor?"{
  controller = "product"
  action = "show"
}

<link:productDetail productName="licorice"
          flavor="strawberry"> Strawberry Licorice </link:productDetail>




                                       43
static mappings = {
   "/product/$id"(controller:"product"){
       action = [GET:"show",
            PUT:"update",
            DELETE:"delete",
            POST:"save"]
   }
}




                         44
import grails.converters.*
class BookController {  

      def list = {   
                                          /book/list
          def books = Book.list()   
          withFormat {    
              html bookList:books            /book/list.json
              json { render books as JSON }   
              xml { render books as XML }   
          }  
      }
                                           /book/list.xml
}

                                                               45
46
47
class BookStoreService {
    
    boolean transactional = true
    
    def allList(params) {
      return Book.list(params)
    }
}


       class BookController {

             def bookStoreService
             def list = {
                 [bookInstanceList:bookStoreService.allList(params),
                     bookInstanceTotal: Book.count()]
             }
                                   48
49
50
mockFor(class, loose = false)
     mockDomain(class, testInstances = )
     mockForConstraintsTests(class, testInstances = )
     mockLogging(class, enableDebug = false)
     mockController(class)
     mockTagLib(class)
                     def testInstances=[]
                     mockDomain(Song, testInstances)
                     assertEquals(0, Song.count())
                     new Song(name:"Supper's Ready").save()
                     assertEquals(1, Song.count())
51
class FooController {
   def someRedirect = { 
     redirect(action:"bar") 
   }
   def text = {      class FooControllerTests extends GrailsUnitTestCase {
     render "bar"      void testText(){ 
   }                     def fc = new FooController() 
 }                       fc.text() 
                         assertEquals "bar", fc.response.contentAsString 
                       }
                       void testSomeRedirect() {
                         def fc = new FooController() 
                         fc.someRedirect() 
                         assertEquals "/foo/bar", fc.response.redirectedUrl 
                       } 
52
53
54
55
56
57
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="127.0.0.1"/>
 </bean>
 <bean id="mailMessageMe" class="org.springframework.mail.SimpleMailMessage">
  <property name="from" value="tyam@xmldo.jp"/>
 </bean>
 <bean id="mailMessageYou" class="org.springframework.mail.SimpleMailMessage">
  <property name="from" value="you@xmldo.jp"/>
 </bean>

                 mailSender(org.springframework.mail.javamail.JavaMailSenderImpl) {
                  host = '127.0.0.1'
                 }
                 def map = ["Me":"tyam@xmldo.jp","You":"you@xmldo.jp"]
                 map.each{k,v->
                  "mailMessage${k}"(org.springframework.mail.SimpleMailMessage) {
                   from = v
                  }
58               }
import org.springframework.context.ApplicationContext
import grails.spring.BeanBuilder
// BeanBuilder
def builder = new BeanBuilder()
builder.beans {
  //
}
// ApplicationContext
ApplicationContext ctx = builder.createApplicationContext()
                                                              59
60
log4j = {
  appenders {
    console name:'stdout',
    layout:pattern(conversionPattern: '%c{2} %m%n')
  }
  error  'org.codehaus.groovy.grails.web.servlet'
}




                 61
62
63
64
65
66
67
grails.plugin.location."auth-domains"="/path/to/modules/auth-domains"

grails.plugin.location."business-domains"="/path/to/modules/business-domains"

grails.plugin.location."common-tags"="/path/to/commons/common-tags"

grails.plugin.location."shop-cart"="/path/to/functionals/shop-cart"


                                      68
%grails maven-install
"
%grails maven-deploy
"
                69
grails.plugin.repos.discovery.local="http://127.0.0.1/gp"
   grails.plugin.repos.distribution.local="http://127.0.0.1/gp"



grails.plugin.repos.discovery.           ="        Http      URL"
grails.plugin.repos.distribution.             ="      Http        URL"

                                    70
app.version=0.1
plugins.acegi=0.5.7
app.servlet.version=2.4
app.grails.version=1.3.RC2
                                           =
plugins.hibernate=1.3.RC2             s.
app.name=relationsample      plugin




                              71
72
73
grails.project.dependency.resolution = {
   ...
   repositories { //
     mavenRepo "http://repository.codehaus.org"
    }
    dependencies {
      runtime 'net.homeip.yusuke:twitter4j:2.0.9'
    }
    ...
                                         ls -al ~/.ivy2/cache
}




% grails install-dependency net.homeip.yusuke:twitter4j:2.0.9

                               74
75
tomcat.deploy.username="manager" 
tomcat.deploy.password="secret" 
tomcat.deploy.url="http://myserver.com/manager"




                         76
77
appenders{null name:'stacktrace'}

                 78
79
80
81
82
83
84

G*ワークショップ in 仙台 Grails(とことん)入門

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    View MVC2 DI 11
  • 12.
  • 13.
    ur ity S ec low Controller i1 8n Flex bF We Codec Quartz Domain Grails lug in JMS MyP Core Quartz ag gin Service UrlMapping T Filters 13
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
    % export GRAILS_HOME=/opt/grails-1.2.2 %export PATH=$GRAILS_HOME/bin:$PATH % grails Welcome to Grails 1.2.2 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: /opt/grails-1.2.2 21
  • 22.
    % grails run-app %grails test-app 22
  • 23.
  • 24.
    |-- application.properties |-- grails-app | |-- conf - | |-- controllers - | |-- domain - | |-- i18n | |-- services - | |-- taglib - | |-- utils | `-- views - |-- lib |-- scripts - |-- src - |-- test - `-- web-app - 24
  • 25.
  • 26.
    package com.pack class Book {   String title   String author   int rate   String comment      Date dateCreated   Date lastUpdated      static constraints = {   } } 26
  • 27.
      static constraints ={     title nullable:false,blank:false     author nullable:false,blank:false     rate range:0..10     comment maxSize:1000     price shared:'myShared'     withTax display:false   } grails.gorm.default.constraints = {     '*'(nullable:true, size:1..20)     myShared(nullable:true) } 27
  • 28.
    class Book implementsSerializable{   static mapping = {     table 'T_BOOK'     id generator:'uuid.hex', params:[type:'string']     columns {       comment column:'F_COMMENT',sqlType:'varchar(1000)'     }     withTax formula: 'price * 1.05'   } 28
  • 29.
  • 30.
    Account.executeQuery(     "select distincta.number from Account a where a.branch = :branch",     [branch:'London'],     [max:10, offset:5] ) Book.findAll("from Book as b where b.author=?",[' ']) 30
  • 31.
    def results =Book.findAllByTitle(" ",     [max:10, sort:"title", order:"desc", offset:100] ) def results = Book.findAllByTitleAndAuthor( " ", " ") 31
  • 32.
    def c =Account.createCriteria() def results = c.list {       like("holderFirstName", "Fred%")       and {            between("balance", 500, 1000)            eq("branch", "London")       }       maxResults(10)     order("holderLastName", "desc") } 32
  • 33.
    dataSource {   pooled =true   driverClassName = "org.hsqldb.jdbcDriver"   username = "sa"   password = "" } hibernate {   cache.use_second_level_cache=true   cache.use_query_cache=true   cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider' } environments {   development {     dataSource {       dbCreate = "update"       url = "jdbc:hsqldb:file:devDB;shutdown=true"     }   }   test {     dataSource { .... .... 33
  • 34.
    import javax.persistence.*    @Entity  @Table(name = "animal")  class Animal {      @Id     @GeneratedValue    int id    String name    @ManyToOne    @JoinColumn    Owner owner      static constraints = {      name blank:false    }  } 34
  • 35.
  • 36.
    http://localhost:8080/myapp/book/index package com.pack // class BookController{     //     def index = {         //         render text:"Hello"     } } 36
  • 37.
    params servletContext,session,request,response beforeInterceptor afterInterceptor def create = {     def bookInstance = new Book()     bookInstance.properties = params     [bookInstance: bookInstance] } def save = {     def bookInstance = new Book(params)     if (bookInstance.save(flush: true)) {         flash.message = " "         redirect(action: "show", id: bookInstance.id)     }     else {         render(view: "create", model: [bookInstance: bookInstance])     } } 37
  • 38.
    class BookController {    def scaffold = true } 38
  • 39.
  • 40.
  • 41.
    class SimpleWikiTagLib {   staticnamespace = "wiki"   static returnObjectForTags = ['content']    def tagname = {attrs, body ->     out<<"Hello"   }      def content = {attrs, body ->     CmsContent.findByCode(attrs.code)?.content   } } 41
  • 42.
    class UrlMappings {   staticmappings = {     "/$controller/$action?/$id?"{       constraints { }     }     "/"(view:"/index")     "500"(view:'/error')     //   URL /blogname/2007/01/10/my_funky_blog_entry     "/$blog/$year?/$month?/$day?/$id?"( controller:"blog", action:"show")     //     "/$blog/$year?/$month?/$day?/$id?" {         controller = "blog"         action = "show"         constraints {             year(matches:/d{4}/)             month(matches:/d{2}/)     //             day(matches:/d{2}/)     "/images/**.jpg"(controller:"image")         }        } 42
  • 43.
    name productDetail:"/showProduct/$productName/$flavor?"{ controller = "product" action = "show" } <link:productDetail productName="licorice" flavor="strawberry"> Strawberry Licorice </link:productDetail> 43
  • 44.
    static mappings ={    "/product/$id"(controller:"product"){        action = [GET:"show",             PUT:"update",             DELETE:"delete",             POST:"save"]    } } 44
  • 45.
    import grails.converters.* class BookController {       def list = {    /book/list         def books = Book.list()            withFormat {                 html bookList:books     /book/list.json             json { render books as JSON }                xml { render books as XML }            }       } /book/list.xml } 45
  • 46.
  • 47.
  • 48.
    class BookStoreService {         boolean transactional = true          def allList(params) {       return Book.list(params)     } } class BookController {     def bookStoreService     def list = {         [bookInstanceList:bookStoreService.allList(params),             bookInstanceTotal: Book.count()]     } 48
  • 49.
  • 50.
  • 51.
    mockFor(class, loose =false) mockDomain(class, testInstances = ) mockForConstraintsTests(class, testInstances = ) mockLogging(class, enableDebug = false) mockController(class) mockTagLib(class) def testInstances=[] mockDomain(Song, testInstances) assertEquals(0, Song.count()) new Song(name:"Supper's Ready").save() assertEquals(1, Song.count()) 51
  • 52.
    class FooController {   def someRedirect = {      redirect(action:"bar")    }   def text = {  class FooControllerTests extends GrailsUnitTestCase {     render "bar"    void testText(){    }     def fc = new FooController()  }     fc.text()      assertEquals "bar", fc.response.contentAsString    }   void testSomeRedirect() {     def fc = new FooController()      fc.someRedirect()      assertEquals "/foo/bar", fc.response.redirectedUrl    }  52
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="127.0.0.1"/> </bean> <bean id="mailMessageMe" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="tyam@xmldo.jp"/> </bean> <bean id="mailMessageYou" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="you@xmldo.jp"/> </bean> mailSender(org.springframework.mail.javamail.JavaMailSenderImpl) { host = '127.0.0.1' } def map = ["Me":"tyam@xmldo.jp","You":"you@xmldo.jp"] map.each{k,v-> "mailMessage${k}"(org.springframework.mail.SimpleMailMessage) { from = v } 58 }
  • 59.
    import org.springframework.context.ApplicationContext import grails.spring.BeanBuilder //BeanBuilder def builder = new BeanBuilder() builder.beans { // } // ApplicationContext ApplicationContext ctx = builder.createApplicationContext() 59
  • 60.
  • 61.
    log4j = {   appenders{     console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')   }   error  'org.codehaus.groovy.grails.web.servlet' } 61
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
    grails.plugin.repos.discovery.local="http://127.0.0.1/gp" grails.plugin.repos.distribution.local="http://127.0.0.1/gp" grails.plugin.repos.discovery. =" Http URL" grails.plugin.repos.distribution. =" Http URL" 70
  • 71.
    app.version=0.1 plugins.acegi=0.5.7 app.servlet.version=2.4 app.grails.version=1.3.RC2 = plugins.hibernate=1.3.RC2 s. app.name=relationsample plugin 71
  • 72.
  • 73.
  • 74.
    grails.project.dependency.resolution = { ... repositories { // mavenRepo "http://repository.codehaus.org" } dependencies { runtime 'net.homeip.yusuke:twitter4j:2.0.9' } ... ls -al ~/.ivy2/cache } % grails install-dependency net.homeip.yusuke:twitter4j:2.0.9 74
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.