Groovy轻松入门——Grails实战基础篇.doc_第1页
Groovy轻松入门——Grails实战基础篇.doc_第2页
Groovy轻松入门——Grails实战基础篇.doc_第3页
Groovy轻松入门——Grails实战基础篇.doc_第4页
Groovy轻松入门——Grails实战基础篇.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Groovy轻松入门Grails实战基础篇一,搭建Grails环境 0,下载Grails( /grails/grails-bin-1.0.zip,请留意朝花夕拾Groovy & Grails中的“最新版本”提示)并解压到自己指定位置(我的位置是D:DMY_DEVgrails)1,设置环境变量GRAILS_HOME(注意大写),过程与“设置环境变量GROOVY_HOME”相似2,将%GRAILS_HOME%bin添加到环境变量path中,过程与“将GROOVY_HOME目录下的bin追加到环境变量path中”相似(如果只想进行Grails开发,可以不设GROOVY_HOME)二,创建Grails Demo程序 3,打开“命令行”,选择当前目录(我的为D:Tempgrails_apps),在黑底白字的窗口中输入“grails create-app demo”,不包括双引号“”,在您的屏幕中可以看到类似下面的输出结果:D:_DEVgrails_appsgrails create-app demoWelcome to Grails 1.0 - /Licensed under Apache Standard License 2.0Grails home is set to: D:DMY_DEVgrails-1.0Base Directory: D:_DEVgrails_appsEnvironment set to developmentNote: No plugin scripts foundRunning script D:DMY_DEVgrails-1.0scriptsCreateApp.groovy mkdir Created dir: D:_DEVgrails_appsdemosrc mkdir Created dir: D:_DEVgrails_appsdemosrcjava mkdir Created dir: D:_DEVgrails_appsdemosrcgroovy mkdir Created dir: D:_DEVgrails_appsdemograils-app mkdir Created dir: D:_DEVgrails_appsdemograils-appcontrollers mkdir Created dir: D:_DEVgrails_appsdemograils-appservices mkdir Created dir: D:_DEVgrails_appsdemograils-appdomain mkdir Created dir: D:_DEVgrails_appsdemograils-apptaglib mkdir Created dir: D:_DEVgrails_appsdemograils-apputils mkdir Created dir: D:_DEVgrails_appsdemograils-appviews mkdir Created dir: D:_DEVgrails_appsdemograils-appviewslayouts mkdir Created dir: D:_DEVgrails_appsdemograils-appi18n mkdir Created dir: D:_DEVgrails_appsdemograils-appconf mkdir Created dir: D:_DEVgrails_appsdemotest mkdir Created dir: D:_DEVgrails_appsdemotestunit mkdir Created dir: D:_DEVgrails_appsdemotestintegration mkdir Created dir: D:_DEVgrails_appsdemoscripts mkdir Created dir: D:_DEVgrails_appsdemoweb-app mkdir Created dir: D:_DEVgrails_appsdemoweb-appjs mkdir Created dir: D:_DEVgrails_appsdemoweb-appcss mkdir Created dir: D:_DEVgrails_appsdemoweb-appimages mkdir Created dir: D:_DEVgrails_appsdemoweb-appWEB-INFclasses mkdir Created dir: D:_DEVgrails_appsdemoweb-appMETA-INF mkdir Created dir: D:_DEVgrails_appsdemolib mkdir Created dir: D:_DEVgrails_appsdemograils-appconfspring mkdir Created dir: D:_DEVgrails_appsdemograils-appconfhibernatepropertyfile Creating new property file: D:_DEVgrails_perties copy Copying 2 files to D:_DEVgrails_appsdemo copy Copying 2 files to D:_DEVgrails_appsdemoweb-appWEB-INF copy Copying 5 files to D:_DEVgrails_appsdemoweb-appWEB-INFtld copy Copying 87 files to D:_DEVgrails_appsdemoweb-app copy Copying 17 files to D:_DEVgrails_appsdemograils-app copy Copying 1 file to D:_DEVgrails_appsdemo copy Copying 1 file to D:_DEVgrails_appsdemo copy Copying 1 file to D:_DEVgrails_appsdemopropertyfile Updating property file: D:_DEVgrails_pertiesCreated Grails Application at D:_DEVgrails_apps/demoD:_DEVgrails_apps通过“grails create-app”这个命令,Grails自动帮我们创建了开发所需的工程环境。其实您现在就已经拥有了一个可运行的Web应用程序,然后进入demo目录(“cd demo”),输入“grails run-app”,回车,启动这个五脏俱全的程序雏形,打开浏览器,输入 http:/localhost:8080/demo ,回车,看到了吧 :) 让我们继续吧,请停止这个程序(Ctrl + C)4,在“命令行”中输入“cd demo”,回车,以进入demo目录,然后再输入“grails create-domain-class User”创建domain class即类似于pojo的pogo,它对应MVC中的Model,不过由Grails自动创建的pogo是空的,需要自己添加属性,约束(constraints)等。输出结果如下所示:D:_DEVgrails_appsdemograils create-domain-class UserWelcome to Grails 1.0 - /Licensed under Apache Standard License 2.0Grails home is set to: D:DMY_DEVgrails-1.0Base Directory: D:_DEVgrails_appsdemoEnvironment set to developmentNote: No plugin scripts foundRunning script D:DMY_DEVgrails-1.0scriptsCreateDomainClass.groovy copy Copying 1 file to D:_DEVgrails_appsdemograils-appdomainCreated for User copy Copying 1 file to D:_DEVgrails_appsdemotestintegrationCreated Tests for UserD:_DEVgrails_appsdemo5,进入D:Tempgrails_appsdemograils-appdomain(这个目录中存放着所有的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)6,在“命令行”中输入“grails generate-all User”,为User产生所有CRUD操作需要的代码(如控制器UserController.groovy)和页面(如list.gsp),输出结果如下所示:D:_DEVgrails_appsdemograils generate-all UserWelcome to Grails 1.0 - /Licensed under Apache Standard License 2.0Grails home is set to: D:DMY_DEVgrails-1.0Base Directory: D:_DEVgrails_appsdemoEnvironment set to developmentNote: No plugin scripts foundRunning script D:DMY_DEVgrails-1.0scriptsGenerateAll.groovy mkdir Created dir: D:_DEVgrails_appsdemoweb-appWEB-INFlib mkdir Created dir: C:Documents and SettingsDaniel.grails1.0projectsdemoclasses groovyc Compiling 7 source files to C:Documents and SettingsDaniel.grails1.0projectsdemoclasses mkdir Created dir: C:Documents and SettingsDaniel.grails1.0projectsdemoresourcesgrails-appi18nnative2ascii Converting 10 files from D:_DEVgrails_appsdemograils-appi18n to C:Documents and SettingsDaniel.grails1.0projectsdemoresourcesgrails-appi18n copy Copying 1 file to C:Documents and SettingsDaniel.grails1.0projectsdemoclasses copy Copying 1 file to C:Documents and SettingsDaniel.grails1.0projectsdemoresources copy Copying 1 file to C:Documents and SettingsDaniel.grails1.0projectsdemo0 spring.GrailsWebApplicationContext Refreshing mons.spring.GrailsWebApplicationContext2b2057: display name mons.spring.GrailsWebApplicationContext2b2057; startup date Tue Feb 05 23:26:45 CST 2008; root of context hierarchy16 spring.GrailsWebApplicationContext Bean factory for application context mons.spring.GrailsWebApplicationContext2b2057: org.springframework.beans.factory.support.DefaultListableBeanFactoryeebf17Generating views for domain class User .Generating controller for domain class User .Finished generation for domain class UserD:_DEVgrails_appsdemo7,修改demograils-appcontrollersUserController.groovy的内容为: class UserController def loginService / 新增的代码 def index = redirect(action:list,params:params) / the delete, save and update actions only accept POST requests def allowedMethods = delete:POST, save:POST, update:POST def list = if(!params.max) params.max = 10 userList: User.list( params ) def show = def user = User.get( params.id ) if(!user) flash.message = User not found with id $params.id redirect(action:list) else return user : user def delete = def user = User.get( params.id ) if(user) user.delete() flash.message = User $params.id deleted redirect(action:list) else flash.message = User not found with id $params.id redirect(action:list) def edit = def user = User.get( params.id ) if(!user) flash.message = User not found with id $params.id redirect(action:list) else return user : user def update = def user = User.get( params.id ) if(user) perties = params if(!user.hasErrors() & user.save() flash.message = User $params.id updated redirect(action:show,id:user.id) else render(view:edit,model:user:user) else flash.message = User not found with id $params.id redirect(action:edit,id:params.id) def create = def user = new User() perties = params return user:user def save = def user = new User(params) if(!user.hasErrors() & user.save() flash.message = User $user.id created redirect(action:show,id:user.id) else render(view:create,model:user:user) / 新增的代码 def login = if (request.method = POST) User u = new User() perties = params if (!u.validate() render(view:login, model:user:u) if ( & params.password) def user = loginService.check(u) if (user) flash.message = Welcome $ render(view: ok, model: user: user) else flash.error = Invalid $ with $u.password render(view: login, model: user: u) else render(view: login, model: user: u) 大家或许也看到了LoginService这个类,我将在后面演示创建它,这个LoginService类封装了所有登陆相关的业务逻辑,Grails会自动将其注入到UserController中8,在“命令行”中,输入“grails create-service Login”,创建LoginService.groovy,输出:D:_DEVgrails_appsdemograils create-service LoginWelcome to Grails 1.0 - /Licensed under Apache Standard License 2.0Grails home is set to: D:DMY_DEVgrails-1.0Base Directory: D:_DEVgrails_appsdemoEnvironment set to developmentNote: No plugin scripts foundRunning script D:DMY_DEVgrails-1.0scriptsCreateService.groovy copy Copying 1 file to D:_DEVgrails_appsdemograils-appservicesCreated Service for Login copy Copying 1 file to D:_DEVgrails_appsdemotestintegrationCreated ServiceTests for LoginD:_DEVgrails_appsdemo9,修改demograils-appservicesLoginService.groovy的内容为:class LoginService boolean transactional = true def check(User u) def user = User.findWhere(name: , password: u.password) return user 10,在demograils-appviewsuser目录下创建login.gsp和ok.gsp,它们对应MVC中的View,内容分别为:login.gsp (复制demograils-appviewsusercreate.gsp的内容到login.gsp中,并修改): Login Home User List Login $flash.error Name: Password: ok.gsp: $flash.messageName: $user?.name Password: $user?.password11,修改demograils-appconfBootStrap.groovy,初始化数据库:将一个User实例保存到数据库(grails自带hsqldb和jetty)中,内容如下:class BootStrap def init = servletContext - new User(name: demo, password: 123456).save() def destroy = 12,在“命令行”中,输入“grails run-app”,运行我们的Web应用,输出如下:D:_DEVgrails_appsdemograils run-appWelcome to Grails 1.0 - /Licensed under Apache Standard License 2.0Grails home is set to: D:DMY_DEVgrails-1.0Base Directory: D:_DEVgrails_appsdemoEnvironment set to developmentNote: No plugin scripts foundRunning script D:DMY_DEVgrails-1.0scriptsRunApp.groovy groovyc Compiling 4 source files to C:Documents and SettingsDaniel.grails1.0projectsdemoclassesRunning Grails application.2008-02-05 23:46:08.912:INFO: Logging to STDERR via org.mortbay.log.StdErrLog2008-02-05 23:46:08.066:INFO: jetty-6.1.42008-02-05 23:46:08.347:INFO: No Transaction manager found - if your webapp requires one, please configure one.2008-02-05 23:46:09.081:/demo:INFO: Set web app root system property: demo = D:_DEVgrails_appsdemoweb-app2008-02-05 23:46:09.081:/demo:INFO: Initializing Log4J from file:C:Documents and SettingsDaniel/.grails/1.0/projects/demo/resources/perties2008-02-05 23:46:09.113:/demo:INFO: Initializing Spring root WebApplicationContext0 spring.GrailsWebApplicationContext Refreshing mons.spring.GrailsWebApplicationContext5facbd: display name mons.spring.GrailsWebApplicationContext5facbd; startup date Tue Feb 05 23:46:14 CST 2008; parent: org.springframework.web.context.support.XmlWebApplicationContext1fef80a0 spring.GrailsWebApplicationContext Bean factory for application context mons.spring.GrailsWebApplicationContext5facbd: org.springframework.beans.factory.support.DefaultListableBeanFactoryaa4c7c2008-02-05 23:46:21.590:/demo:INFO: Initializing Spring FrameworkServ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论