Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

3,293 views

Published on

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

  • Be the first to comment

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

  1. 1. def speaker = new Cast(name:"T.Yamamoto",version:"G*-2010-04-24") 1
  2. 2. 2
  3. 3. 3
  4. 4. 4
  5. 5. 5
  6. 6. 6
  7. 7. 7
  8. 8. 8
  9. 9. 9
  10. 10. 10
  11. 11. View MVC2 DI 11
  12. 12. 12
  13. 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. 14. 14
  15. 15. 15
  16. 16. 16
  17. 17. 17
  18. 18. 18
  19. 19. 19
  20. 20. 20
  21. 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. 22. % grails run-app % grails test-app 22
  23. 23. 23
  24. 24. |-- application.properties |-- grails-app | |-- conf - | |-- controllers - | |-- domain - | |-- i18n | |-- services - | |-- taglib - | |-- utils | `-- views - |-- lib |-- scripts - |-- src - |-- test - `-- web-app - 24
  25. 25. 25
  26. 26. package com.pack class Book {   String title   String author   int rate   String comment      Date dateCreated   Date lastUpdated      static constraints = {   } } 26
  27. 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. 28. 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. 29. 29
  30. 30. 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
  31. 31. def results = Book.findAllByTitle(" ",     [max:10, sort:"title", order:"desc", offset:100] ) def results = Book.findAllByTitleAndAuthor( " ", " ") 31
  32. 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. 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. 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. 35. 35
  36. 36. http://localhost:8080/myapp/book/index package com.pack // class BookController {     //     def index = {         //         render text:"Hello"     } } 36
  37. 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. 38. class BookController {     def scaffold = true } 38
  39. 39. 39
  40. 40. 40
  41. 41. class SimpleWikiTagLib {   static namespace = "wiki"   static returnObjectForTags = ['content']    def tagname = {attrs, body ->     out<<"Hello"   }      def content = {attrs, body ->     CmsContent.findByCode(attrs.code)?.content   } } 41
  42. 42. 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
  43. 43. name productDetail:"/showProduct/$productName/$flavor?"{ controller = "product" action = "show" } <link:productDetail productName="licorice" flavor="strawberry"> Strawberry Licorice </link:productDetail> 43
  44. 44. static mappings = {    "/product/$id"(controller:"product"){        action = [GET:"show",             PUT:"update",             DELETE:"delete",             POST:"save"]    } } 44
  45. 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. 46. 46
  47. 47. 47
  48. 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. 49. 49
  50. 50. 50
  51. 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. 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. 53. 53
  54. 54. 54
  55. 55. 55
  56. 56. 56
  57. 57. 57
  58. 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. 59. import org.springframework.context.ApplicationContext import grails.spring.BeanBuilder // BeanBuilder def builder = new BeanBuilder() builder.beans { // } // ApplicationContext ApplicationContext ctx = builder.createApplicationContext() 59
  60. 60. 60
  61. 61. log4j = {   appenders {     console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')   }   error  'org.codehaus.groovy.grails.web.servlet' } 61
  62. 62. 62
  63. 63. 63
  64. 64. 64
  65. 65. 65
  66. 66. 66
  67. 67. 67
  68. 68. 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
  69. 69. %grails maven-install " %grails maven-deploy " 69
  70. 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. 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. 72. 72
  73. 73. 73
  74. 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. 75. 75
  76. 76. tomcat.deploy.username="manager"  tomcat.deploy.password="secret"  tomcat.deploy.url="http://myserver.com/manager" 76
  77. 77. 77
  78. 78. appenders{null name:'stacktrace'} 78
  79. 79. 79
  80. 80. 80
  81. 81. 81
  82. 82. 82
  83. 83. 83
  84. 84. 84

×