科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Groovy轻松入门——Grails实战基础篇

Groovy轻松入门——Grails实战基础篇

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一

作者:山风小子 来源:蛟龍居 2007年11月18日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

通过“grails create-app”这个命令,Grails自动帮我们创建了开发所需的工程环境。其实您现在就已经拥有了一个可运行的Web应用程序,然后进入demo目录(“cd demo”),输入“grails run-app”,回车,启动这个‘五脏俱全’的程序雏形,打开浏览器,输入 http://localhost:8080/demo ,回车,看到了吧 :) 让我们继续吧,请停止这个程序(Ctrl + C)

1,进入demo目录,在“命令行”中输入“cd demo”,回车,然后再输入“grails create-domain-class User”创建domain class即类似于pojo的pogo,它对应MVC中的Model,不过由Grails自动创建的pogo是空的,需要自己添加属性,约束(constraints)等。输出结果如下所示:

D:\Temp\grails_apps\demo>grails create-domain-class User

Welcome to Grails 0.4.2 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:\D\MY_DEV\grails

Base Directory: D:\Temp\grails_apps\demo
Environment set to production
Running script D:\D\MY_DEV\grails\scripts\CreateDomainClass.groovy
     [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-app\domain
Created  at D:\Temp\grails_apps\demo/grails-app/domain/User.groovy
     [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-tests
Created Tests at D:\Temp\grails_apps\demo/grails-tests/UserTests.groovy
D:\Temp\grails_apps\demo>

2,进入D:\Temp\grails_apps\demo\grails-app\domain(这个目录中存放着所有的domain class),打开User.groovy,修改为如下内容:

class   User { 
    String name
    String password
    
    String toString() {
         " $name : $password "     
    }
    
     static  constraints  =  {
        name(blank:  false )    
        password(blank:  false , size:  6 .. 16 )
    }
contraints这个类变量是定义一些约束的,比如name不能为空白,password不能为空白而且长度在6到16之间(包括6和16)
3,在“命令行”中输入“grails generate-all User”,为User产生所有CRUD操作需要的代码(如控制器UserController.groovy)和页面(如list.gsp),输出结果如下所示:
D:\Temp\grails_apps\demo>grails generate-all User

Welcome to Grails 0.4.2 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: D:\D\MY_DEV\grails

Base Directory: D:\Temp\grails_apps\demo
Environment set to production
Running script D:\D\MY_DEV\grails\scripts\GenerateAll.groovy
Compiling sources
   [delete] Deleting directory D:\Temp\grails_apps\demo\web-app\WEB-INF\grails-a
pp
    [mkdir] Created dir: D:\Temp\grails_apps\demo\web-app\WEB-INF\grails-app\vie
ws
    [mkdir] Created dir: D:\Temp\grails_apps\demo\web-app\WEB-INF\grails-app\i18
n
     [copy] Copying 2 files to D:\Temp\grails_apps\demo\web-app\WEB-INF\grails-a
pp\views
     [copy] Copied 2 empty directories to 1 empty directory under D:\Temp\grails
_apps\demo\web-app\WEB-INF\grails-app\views
[native2ascii] Converting 1 file from D:\Temp\grails_apps\demo\grails-app\i18n t
o D:\Temp\grails_apps\demo\web-app\WEB-INF\grails-app\i18n
Attempting to load [13] core plugins
Grails plug-in [i18n] with version [0.4.2] loaded successfully
Grails plug-in [quartz] with version [0.4.2] loaded successfully
Grails plug-in [core] with version [0.4.2] loaded successfully
Grails plug-in [domainClass] with version [0.4.2] loaded successfully
Grails plug-in [controllers] with version [0.4.2] loaded successfully
Attempting to load [0] user defined plugins
Grails plug-in [dataSource] with version [0.4.2] loaded successfully
Grails plug-in [codecs] with version [0.4.2] loaded successfully
Grails plug-in [hibernate] with version [0.4.2] loaded successfully
Grails plug-in [services] with version [0.4.2] loaded successfully
Grails plug-in [scaffolding] with version [0.4.2] loaded successfully
Generating views for domain class [User]
Generating list view for domain class [User]
list view generated at D:\Temp\grails_apps\demo\.\grails-app\views\user\list.gsp

Generating show view for domain class [User]
Show view generated at D:\Temp\grails_apps\demo\.\grails-app\views\user\show.gsp

Generating edit view for domain class [User]
Edit view generated at D:\Temp\grails_apps\demo\.\grails-app\views\user\edit.gsp

Generating create view for domain class [User]
Create view generated at D:\Temp\grails_apps\demo\.\grails-app\views\user\create
.gsp
Generating controller for domain class [User]
Controller generated at .\grails-app\controllers\UserController.groovy
D:\Temp\grails_apps\demo>
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章