微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

通过“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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐