First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評論-1047  文章-0  trackbacks-0

          安裝好Grails后,通過下面幾步工作就可以創建一個簡單的Grails項目,從中來體驗下Grails多帶來的便捷。
          創建一個Grails項目:
          grails create-app
          提示輸入程序名的時候輸入你希望的名字(本例用的是project)
          create-app:
          [input] Enter application name:
          project
          生成的項目結構如下

          /opt/grails/project
          .
          |-grails-app —>(對應ruby on rails的app目錄)
          |—conf —>放置配置文件,例如數據源的配置文件(對應ror的config目錄)
          |—controllers —>放置controller文件
          |—domain —>放置domain class文件(對應ror的model目錄)
          |—i18n
          |—services
          |—taglib
          |—views —>放置views文件(對應ruby on rails的views目錄)
          |—–layouts —>放置layouts文件(對應ruby on rails的layouts目錄)
          |-grails-tests —>和grails-app對應的,這個是測試文件的目錄(對應ror的test)
          |-hibernate —>hibernate配置信息目錄(可選)
          |-lib
          |-spring —>spring配置信息目錄(可選)
          |-src
          |—groovy
          |—java
          |-web-app
          |—css
          |—–tree
          |——-check
          |——-default
          |——-folders
          |——-menu
          |—images
          |—–tree
          |——-check
          |——-default
          |——-folders
          |——-menu
          |—js
          |—–prototype
          |—–yahoo
          |——-assets
          |—META-INF
          |—WEB-INF
          |—–classes
          |—–tld

          配置數據源:(這步在我們的快速入門里可以暫時忽略)
          Grails在grails-app/conf下創建了三個數據源配置文件,DevelopmentDataSource, TestDataSource, and ProductionDataSource分別對應了三種標準的環境
          Grails默認幫我們設置了HSQLDB數據庫(使用內存模式),這個有利于測試,但對實際部署沒有多少意義。可以方便替換成想要的數據源,只要做相應的修改,并把所需的數據庫驅動類庫放入lib目錄就可以了。
          創建Domain Class:
          cd project grails create-domain-class
          提示的時候輸入你希望的domain class名(以book為例),Grails會在grails-app/domain目錄下創建Book.groovy,此時Book.groovy的內容為:
          class Book { }
          下面給book添加屬性,
          vi grails-app/domain/Book.groovy
          class Book { Long id Long version String title String author }
          book對應的是數據庫中的book表,Book中的屬性對應的是book表中列,book表以及列會通過GORM自動生成
          下面有必要創建些測試數據:
          vi grails-app/conf/ApplicationBootStrap.groovy
          class ApplicationBootStrap { def init = { servletContext -> // Create some test data new Book(author:"Stephen King",title:"The Shining").save() new Book(author:"James Patterson",title:"Along Came a Spider").save() } def destroy = { } }
          創建Controller:
          grails generate-all
          生成grails-app/controllers/BookController.groovy
          運行Grails:
          grails run-app
          Grails默認的測試環境是Jetty,默認端口是8080(這也許會和你的tomcat沖突),你可以自己指定端口,比如:
          grails -Dserver.port=9090 run-app
          下面在瀏覽器里看下你的成果http://localhost:8080/project/book/list 或者http://localhost:8080/project/book/(list是BookController的默認動作)。

          此處截圖就省略了。
          感覺是比Ruby on Rails快。

          原文地址:http://blog.rubypdf.com/2006/10/18/grails%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8%EF%BC%88quick-start%EF%BC%89/

          附:Groovy與Grails同甘共苦,苦盡甘來

          posted on 2007-04-22 22:03 山風小子 閱讀(1620) 評論(0)  編輯  收藏 所屬分類: Groovy & Grails
          主站蜘蛛池模板: 阳江市| 淮南市| 宿州市| 喀喇| 昌吉市| 广南县| 理塘县| 宜春市| 阿勒泰市| 盐城市| 榆林市| 东乡县| 谷城县| 钦州市| 朝阳县| 大安市| 邵东县| 连平县| 汝城县| 武夷山市| 上蔡县| 滁州市| 宽甸| 广州市| 东辽县| 浪卡子县| 衡阳县| 井陉县| 普格县| 兴化市| 筠连县| 扎兰屯市| 元谋县| 武清区| 铜梁县| 墨脱县| 赞皇县| 尚志市| 乌兰浩特市| 临泽县| 平乡县|