薄薄的Java
          技術本來很廣,但我們可以把它變薄!
          posts - 0,  comments - 2,  trackbacks - 0
          Grails是個典型的遵照MVC模式開發的框架,至少包含了以下3個部分:控制器(Controllers)、領域(Domain)對象(模型)、視圖(Views)。既然環境配置好了,Hello項目的基本構架也通過grails create-app hello創建好了,下面進入第一個grails例子。
           
          進入hello項目文件夾(>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目錄下自動生成HelloController.groovy文件,同時grails-test下生成HelloControllerTest.groovy測試文件。
          我們編輯HelloController控制器,在index{}中輸出Hello World.其中,index是系統默認的操作,相當于JSP中的index.jsp。
          1 class HelloController {
          2     def index = {render 'Hello world!' }
          3 }
          運行項目 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
          打開瀏覽器,輸入http://localhost:8080/hello/hello/index 查看運行結果

          地址欄中,第一個hello為項目名,第二hello為控制器名,index為操作名。

          最基本的一個入門程序寫完了,除了手工編寫外,我們還可以使用NetBeans作為開發IDE,大大簡化開發流程。至少不用記那么多命令了哈!

          posted on 2009-03-12 18:12 Rique 閱讀(1123) 評論(0)  編輯  收藏 所屬分類: Groovy涉獵

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿

          文章分類

          文章檔案

          收藏夾

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 囊谦县| 井冈山市| 郁南县| 海门市| 图木舒克市| 太康县| 兴业县| 田东县| 阳春市| 沛县| 化德县| 牡丹江市| 玉树县| 宁城县| 山阳县| 蓬莱市| 宣武区| 株洲县| 灵丘县| 汕尾市| 临西县| 淮北市| 桃园县| 邵武市| 海丰县| 深水埗区| 竹溪县| 盖州市| 恩施市| 黑水县| 河西区| 巴彦淖尔市| 钟祥市| 湘乡市| 内乡县| 河源市| 连云港市| 沙坪坝区| 沂南县| 昌图县| 平安县|