薄薄的Java
          技術(shù)本來(lái)很廣,但我們可以把它變薄!
          posts - 0,  comments - 2,  trackbacks - 0
          Grails是個(gè)典型的遵照MVC模式開(kāi)發(fā)的框架,至少包含了以下3個(gè)部分:控制器(Controllers)、領(lǐng)域(Domain)對(duì)象(模型)、視圖(Views)。既然環(huán)境配置好了,Hello項(xiàng)目的基本構(gòu)架也通過(guò)grails create-app hello創(chuàng)建好了,下面進(jìn)入第一個(gè)grails例子。
           
          進(jìn)入hello項(xiàng)目文件夾(>cd hello),輸入 grails create-controller hello
           1E:\Groovy\projects\hello>grails create-controller hello
           2
           3Welcome to Grails 1.0.4 - http://grails.org/
           4Licensed under Apache Standard License 2.0
           5Grails home is set to: E:\Groovy\grails-1.0.4        
           6        
           7Base Directory: E:\Groovy\projects\hello
           8Note: No plugin scripts found
           9Running script E:\Groovy\grails-1.0.4\scripts\CreateController.groovy
          10Environment set to development
          11     [copy] Copying 1 file to E:\Groovy\projects\hello\grails-app\controllers
          12Created Controller for Hello
          13    [mkdir] Created dir: E:\Groovy\projects\hello\grails-app\views\hello
          14     [copy] Copying 1 file to E:\Groovy\projects\hello\test\integration
          15Created ControllerTests for Hello
          這樣,在grails-app/controller目錄下自動(dòng)生成HelloController.groovy文件,同時(shí)grails-test下生成HelloControllerTest.groovy測(cè)試文件。
          我們編輯HelloController控制器,在index{}中輸出Hello World.其中,index是系統(tǒng)默認(rèn)的操作,相當(dāng)于JSP中的index.jsp。
          1 class HelloController {
          2     def index = {render 'Hello world!' }
          3 }
          運(yùn)行項(xiàng)目 grails run-app
           1E:\Groovy\projects\hello>grails run-app
           2
           3Welcome to Grails 1.0.4 - http://grails.org/
           4Licensed under Apache Standard License 2.0
           5Grails home is set to: E:\Groovy\grails-1.0.4        
           6        
           7Base Directory: E:\Groovy\projects\hello
           8Note: No plugin scripts found
           9Running script E:\Groovy\grails-1.0.4\scripts\RunApp.groovy
          10Environment set to development
          11  [groovyc] Compiling 7 source files to C:\Documents and Settings\Administrator\.grails\1.0.4\projects\hello\classes
          12[native2ascii] Converting 11 files from E:\Groovy\projects\hello\grails-app\i18n to C:\Documents and Settings\Administrator\.grails\1.0.4\projects\hello\resources\grails-app\i18n
          13     [copy] Copying 1 file to C:\Documents and Settings\Administrator\.grails\1.0.4\projects\hello\classes
          14     [copy] Copying 1 file to C:\Documents and Settings\Administrator\.grails\1.0.4\projects\hello\resources
          15     [copy] Copying 1 file to C:\Documents and Settings\Administrator\.grails\1.0.4\projects\hello
          16Running Grails application..
          172009-03-12 20:00:01.183::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
          182009-03-12 20:00:01.402::INFO:  jetty-6.1.12
          192009-03-12 20:00:02.652::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
          202009-03-12 20:00:02.308:/hello:INFO:  Set web app root system property: 'hello-development-0.1= [E:\Groovy\projects\hello\web-app]
          212009-03-12 20:00:02.308:/hello:INFO:  Initializing log4j from [file:C:\Documents and Settings\Administrator/.grails/1.0.4/projects/hello/resources/log4j.properties]
          222009-03-12 20:00:02.340:/hello:INFO:  Initializing Spring root WebApplicationContext
          23[0] spring.GrailsWebApplicationContext Refreshing org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@396c7: display name [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@396c7]; startup date [Thu Mar 12 20:00:05 CST 2009]; parent: org.springframework.web.context.support.XmlWebApplicationContext@1e7c609
          24[0] spring.GrailsWebApplicationContext Bean factory for application context [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@396c7]: org.springframework.beans.factory.support.DefaultListableBeanFactory@c4c05
          252009-03-12 20:00:08.179:/hello:INFO:  Initializing Spring FrameworkServlet 'grails'
          262009-03-12 20:00:09.445::INFO:  Started SelectChannelConnector@0.0.0.0:8080
          27Server running. Browse to http://localhost:8080/hello
          打開(kāi)瀏覽器,輸入http://localhost:8080/hello/hello/index 查看運(yùn)行結(jié)果

          地址欄中,第一個(gè)hello為項(xiàng)目名,第二hello為控制器名,index為操作名。

          最基本的一個(gè)入門(mén)程序?qū)懲炅耍耸止ぞ帉?xiě)外,我們還可以使用NetBeans作為開(kāi)發(fā)IDE,大大簡(jiǎn)化開(kāi)發(fā)流程。至少不用記那么多命令了哈!

          posted on 2009-03-12 18:12 Rique 閱讀(1128) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Groovy涉獵

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿

          文章分類(lèi)

          文章檔案

          收藏夾

          搜索

          •  

          最新評(píng)論

          主站蜘蛛池模板: 闻喜县| 北宁市| 德化县| 滨州市| 禹城市| 武乡县| 南郑县| 泰顺县| 城口县| 克什克腾旗| 晋州市| 福州市| 肇庆市| 安陆市| 丹凤县| 酒泉市| 丰台区| 吉林市| 铁力市| 郑州市| 东平县| 武安市| 米易县| 贵港市| 儋州市| 延吉市| 满洲里市| 台北县| 封开县| 元江| 新密市| 同德县| 武汉市| 徐水县| 孟州市| 镇安县| 巴林右旗| 龙海市| 枣阳市| 上蔡县| 永城市|