薄薄的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 阅读(1100) 评论(0)  编辑  收藏 所属分类: Groovy涉猎

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

留言簿

文章分类

文章档案

收藏夹

搜索

  •  

最新评论