進入hello項目文件夾(>cd hello),輸入 grails create-controller hello
1
E:\Groovy\projects\hello>grails create-controller hello
2
3
Welcome to Grails 1.0.4 - http://grails.org/
4
Licensed under Apache Standard License 2.0
5
Grails home is set to: E:\Groovy\grails-1.0.4
6
7
Base Directory: E:\Groovy\projects\hello
8
Note: No plugin scripts found
9
Running script E:\Groovy\grails-1.0.4\scripts\CreateController.groovy
10
Environment set to development
11
[copy] Copying 1 file to E:\Groovy\projects\hello\grails-app\controllers
12
Created 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
15
Created ControllerTests for Hello
這樣,在grails-app/controller目錄下自動生成HelloController.groovy文件,同時grails-test下生成HelloControllerTest.groovy測試文件。
2

3

4

5

6

7

8

9

10

11

12

13

14

15

我們編輯HelloController控制器,在index{}中輸出Hello World.其中,index是系統默認的操作,相當于JSP中的index.jsp。
1 class HelloController {
2 def index = {render 'Hello world!' }
3 }
運行項目 grails run-app2 def index = {render 'Hello world!' }
3 }
1
E:\Groovy\projects\hello>grails run-app
2
3
Welcome to Grails 1.0.4 - http://grails.org/
4
Licensed under Apache Standard License 2.0
5
Grails home is set to: E:\Groovy\grails-1.0.4
6
7
Base Directory: E:\Groovy\projects\hello
8
Note: No plugin scripts found
9
Running script E:\Groovy\grails-1.0.4\scripts\RunApp.groovy
10
Environment 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
16
Running Grails application..
17
2009-03-12 20:00:01.183::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
18
2009-03-12 20:00:01.402::INFO: jetty-6.1.12
19
2009-03-12 20:00:02.652::INFO: No Transaction manager found - if your webapp requires one, please configure one.
20
2009-03-12 20:00:02.308:/hello:INFO: Set web app root system property: 'hello-development-0.1' = [E:\Groovy\projects\hello\web-app]
21
2009-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]
22
2009-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
25
2009-03-12 20:00:08.179:/hello:INFO: Initializing Spring FrameworkServlet 'grails'
26
2009-03-12 20:00:09.445::INFO: Started SelectChannelConnector@0.0.0.0:8080
27
Server running. Browse to http://localhost:8080/hello
打開瀏覽器,輸入http://localhost:8080/hello/hello/index 查看運行結果
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

地址欄中,第一個hello為項目名,第二hello為控制器名,index為操作名。
最基本的一個入門程序寫完了,除了手工編寫外,我們還可以使用NetBeans作為開發IDE,大大簡化開發流程。至少不用記那么多命令了哈!