




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、1、下載安裝jdk 1.5或者更高版本,下載安裝grails2、創(chuàng)建.bat文件,文件:set java_home=c:javajdkl.6.0_13 setgroovy_home=e:2ggroovy-1.6.3set path=%java_home%/bin;%groovy_home%/bin;%path% cmd在控制臺下輸入grails help,如果能看到grails的命令列表,grails的安裝配置就 算成功了。圖3.檢驗(yàn)grails是否安裝成功cc c:windowssystem32cmd. exe.qehc:xixocunents and settingsliang$hixin
2、g>graiis help噸ueleone to gkaiis 1.0-rc1 一 http:// licensed under apache standard license 2.0 grails hone is set to: d:g>*ails-1.0-rc1base directory: c:docurcnts and settings1iangshixing environnent set to developmentnote: no plugin scripts foundrunning script d:grailsl.0-rc1scriptshel
3、p.groovy<optionals narked with *>: grails lenuironnent j* (target 】(arunentsexamples: grails dev run-app grails create -app booksavailable targets <type grails belp 1 target-name1info >hgrails bootstrap grails bug-report grails clean grails compile grails console13、創(chuàng)建一個grails應(yīng)用程序創(chuàng)建grails
4、應(yīng)用程序需要使用grails create-app命令,在控制臺輸入:graiis create-app contactgrails會為我們創(chuàng)建一個名叫“contact”文件夾,并在其中生成一個應(yīng)用程序框架, 生成的應(yīng)用程序目錄結(jié)構(gòu)如下:表1. contact應(yīng)用文件目錄結(jié)構(gòu)contact+ grails-app+ conf存放配置信息,包含數(shù)據(jù)源、應(yīng)用程序啟動時自動執(zhí)行的類applicationbootstrap.groovy, url 映射配置+ con troller+ domai n存放控制器(“mvc”的“c” )存放域類(“mvc”的“m “)+ i18n+ services+ t
5、aglib+ views存放國際化資源文件存放service類存放標(biāo)簽庫類存放視圖模版(“mvc”的v,每個控制器對應(yīng)一個文件夾并存放在views中,每個文件夾中會有多個gsp頁面)+ layouts存放布局模板+ grails-tests+ hiber nate+ lib+ spring存放測試代碼存放可選的其它hibernate配置文件 存放其它jar包(如jdbc驅(qū)動等) 存放可選的spring配置文件+ src+ java+ groovy存放java源程序存放groovy源程序+ web-app+ css+ images+ js+ web-inf+ in dex.gsp存放css樣式表
6、存放圖片文件存放javascript文件 存放部署相關(guān)的文件 應(yīng)用程序的首頁從上述目錄結(jié)構(gòu)可以看岀grails的“約定優(yōu)于配置”設(shè)計(jì)理念,它約定了不同層次代 碼存放的位置以及項(xiàng)目的組織方式,這既提供了一種最佳實(shí)踐,同時也節(jié)省了開發(fā)人員 配置項(xiàng)目的精力。添加 domain class接下來,為剛創(chuàng)建好的項(xiàng)冃添加一些功能。首先創(chuàng)建兩個domainclass。前面說過,domainclass實(shí)質(zhì)上是數(shù)據(jù)庫表映射的實(shí)體類。通過控制臺,進(jìn)入項(xiàng)目的根文件夾(注意:本 文出現(xiàn)一切的控制臺輸入,除create-app夕卜,都需要在項(xiàng)目文件夾中執(zhí)行),輸入grails create-domain-class t
7、eam圖 4.創(chuàng)建 domain classm c:windowssysteni32cmd. exed: xflygroo v program wo tks pac e xcon t ac t >gra i is create-dona in-c lass teamuelcome to grails 0<6 - http:/grails<org/licensed under apache standard license 2.0grails hone is set to: d: jrails-0.6basc directory: d:mygroouyprogranxu#ork
8、spdcccont<tctenvironncnt set to dcvelopncntnote: no plugin scripts foundrunning script d:oa!1s-06sclaptsxcfe/ttedofn&lncla£$.yzoouy【copy】 copying 1 file to d:mygroovyprograrworkspacccontttct0rails-appxdo maincreated fo>* tean(copy copying 1 file to d:xrtygroovyprcgr«nworkspacecon
9、tact:testintegrat ioncreated tests for teand:hygroovyprogranworkspacecontact >w新創(chuàng)建的domain類出現(xiàn)在了 grails-app/domain文件夾中,同時grails還創(chuàng)建了相 應(yīng)的test類。用記事本打開grails-appdomain文件夾中的team.groovy,加入如下內(nèi)容:class team string teamnamedate founddate這兩行代碼描述了 team所包含的屬性,從代碼中可以看出groovy語言的一些特點(diǎn):1. 無需“;”結(jié)尾(有也不會報(bào)錯);2. 簡單數(shù)據(jù)類型,與
10、java相同;創(chuàng)建 controller接下來為domain class team類創(chuàng)建controller,在控制臺輸入grails create-controller teamgrails 會在 grails-app/controller 中創(chuàng)建一個名為 teamcontroller 的類。編輯teamcontroller.groovy文件。加入如下代碼:class teamcontroller def scatfoid = team不要小看這僅有的一行代碼,它使team表有了相應(yīng)的crud (增、刪、查、改)功 能的頁面。在控制臺中輸入grails run-app,運(yùn)行應(yīng)用以查看效果:g
11、railsc monwbhew lemnlist>>>m hemr“2(droswj petet2007640 00x00.0t86:004圖 6. show team pagej. grailshome; team listnev/ teamshow teamid:found date:2007-09-10 00:36:00.0db2team name:圖 7. edit team page爲(wèi) home _ team list 門 nev/ teamedit teamfound date:team name:10v九月.2007v005/36 7db2冃 update 上
12、delete一些疑問前面的例子演示了如何使用grails快速開發(fā)一個應(yīng)用。如果使用struts之類的javaweb框架開發(fā)一個類似上述的功能可能要花幾個小時的時間,而使用grails只用了不到五分鐘 可以完成了創(chuàng)建。對grails的“快”您應(yīng)該有了一個比較直觀的感受了吧?雖然現(xiàn)在程序己經(jīng)實(shí)現(xiàn)了對team表的crud操作,但讀者可能會有如下的幾個疑 問:1.數(shù)據(jù)庫在哪?2如何修改頁面的外觀?3. url與controller的對應(yīng)關(guān)系是什么?4. 表單能做驗(yàn)證嗎?配置數(shù)據(jù)庫實(shí)現(xiàn)了 crud,為什么沒有配置數(shù)據(jù)庫呢?grails為用戶提供的一個內(nèi)置的輕量級數(shù)據(jù)庫hsqldb,雖然在性能和功能上難以
13、滿足 要求,但是對于演示scaffold還是綽綽有余的。當(dāng)然,我們可以用mysql去替換它,下面就來 演示這個過程(如果讀者不想使用mysql去替換hsqldb,可以跳過下面的內(nèi)容)。首先修改 grails-appconfdatasource.groovy:datasource pooied = falsedriverclassname = '"com .mysq l jdbc .driver"user name = ,'rootmpassword = m*mhibernate cache use_second_levei_cache=t ruecacheu
14、se_query_cache=vider_ciass=worg.h i be mate cache ehcacheprovider*/ environment specific settingsenvironments development datasource dbcreate = "update” / one of create*, *create-dropw,update* url = ,fjdbc: mysq i:/1 oca i host :3306/contact_devmtest datasource dbc reate = 'upd
15、ate'*url = ,fjdbc: mysq i:/1 oca i host :3306/contact_test,fproduction datasource dbcreate = "update”url = jdbc:mysqi :/localhost:3306/contact_prod,fgrails默認(rèn)要求提供三個數(shù)據(jù)庫,分別用于開發(fā)、測試和產(chǎn)品環(huán)境。修改連接的driver 名稱、用戶名密碼以及三個數(shù)據(jù)庫的url,保存。接下來分別創(chuàng)建上述三個數(shù)據(jù)庫,并 把mysql的jdbc驅(qū)動程序拷貝到lib文件夾中。接下來,重新運(yùn)行項(xiàng)目(ggiis run-epp) , gra
16、ils會自動為domain class創(chuàng)建相 應(yīng)的數(shù)據(jù)庫表。至此,team的crud程序就運(yùn)行在mysql z上了。修改頁面外觀使用scaffold實(shí)現(xiàn)的crud實(shí)際上并沒有創(chuàng)建gsp頁面,所以無法定制view內(nèi) 容。在grails中,可以使用grails generate-all命令為指定的domain class創(chuàng) 建實(shí)現(xiàn)crud操作的controller和view。所以,在控制臺運(yùn)行(如果提示teamcontroller已存在,是否 覆蓋?請選”是”):graiis generate-ali teamgrails會在grails-appviews目錄中創(chuàng)建一個名叫team的文件夾,文件夾
17、的名稱剛 好和controller對應(yīng)。team文件夾中包含了 4個gsp文件,分別是create.gsplist.gspedit.gsp和show.gsp。這幾個頁面和之前scaffold實(shí)現(xiàn)的功能是一致的。修改這幾個 頁面的代碼,即可以定制頁面的外觀。url與controller的命名約定打開 generate-aii 命令產(chǎn)生的 controller 即 teamcontroller.groovy 文件,可以看 到如下內(nèi)容:class teamcontroller def index = redirect(action:iist,params:params) / the delete,
18、save and update actions only accept post requestsdef allowedmethods = delete:wpostw, save:wpostw, update:wpostdef list = if(!params.max) params.max = 10teamlist: team.iist( params )def show = team : team.get( params.id ).其中def xxx = xxx的結(jié)構(gòu)在groovy語言中叫閉包(closure),在controller中, 每個閉包對應(yīng)為一個action,即處理一個特定的
19、web請求。然后看看grails url的 命名原則:http(s):/host:pot/projectname/contro11ername/act ionname/paramete表單的驗(yàn)證grails對表單的驗(yàn)證提供了強(qiáng)大的支持,可以使用domain class的驗(yàn)證來實(shí)現(xiàn)表單的驗(yàn)證。修改team.groovy文件:class team string teamnamedate founddatestatic constraints =teamname(size:350,blank:false,unique:true)在team類中加入個名叫constraints的靜態(tài)closure,并在
20、其中描述驗(yàn)證邏輯: 怕am的字符串長度在3-50之間、不能為空、而且是唯一的。(注意早期版木的grails 使用length限定字符串的長度,在v0.5以后改為了 size保存team.groovy,重 新運(yùn)行contact,會發(fā)現(xiàn)驗(yàn)證邏輯已經(jīng)可以工作了:圖8.表單驗(yàn)證grailshome 衛(wèi) team listcreate team* class team?的區(qū)哇tzmname蘭三grails teamjl是逅一王team name:found date:24 v但驗(yàn)證失敗后報(bào)出的錯誤信息對用戶來說并不友好。想要修改這些內(nèi)容,需要編輯grailsappi18n中的資源文件使用grails快速
21、開發(fā)web應(yīng)用程序使用eclipse搭建開發(fā)環(huán)境下面主要介紹groovy插件的安裝方法:首先啟動 eclipse,點(diǎn)擊 help > software updates > find and install -: 圖9.更新和安裝*| hieri七 kavixoutli«" j 6* 1 iji . (b 9i clotn 史 ccjl iba. fi ai is.forwr seaxehxenbetcc>mwid. crovys hibyrnatr 今 s> iprir:z bootst2p"gvy£ ccnfic“gvy
22、3; dstsqurce*exit e<ht souter%*xch erojct bun juvsowr java 一 contact/grails app/domain/team. groovy 一 eclipse sdk9 1 * 4幽跑3ii urersc 伙】p content 1瞥 sjwchkey amrt. ctrlshifth,i、p= ioa tracks.4cheat sheetr.<about eelipre sdk料my c«if “ue ionsa” hlp在彈出窗口 屮選擇 search for new features to instali
23、 點(diǎn)擊 next: 圖10.查找新特性& instal1/update,cub| wbv 1 11feature updateschoose the way you want to search for features to installosearch for updates of the currently instailed featuresselect this option if you vant to search for updates of the features you already have inst ailed.®search for nev fe
24、atures to installselect this option if you want to install ne featpires from existing or nev update sites. sqm sites may already be available- you c|xadd new update site urls to the search.kk;然后在新窗口中點(diǎn)擊new remote site.,并在彈出窗口中輸入:name: groovyurl: http:/dist codehaus org/groovy/(jistribut:ions/updatede
25、v/ 圖 11添加 classpath variablestype filter textk cenex4± ante cwuwct ivity "oovy> help> inrt intarnet-jiva 壬 appearance is build p>thclupath vax oxer librarie g cod* style compiler dbuhit令 debut editor installed jr£f junitp“x代 & fj” jpat pluc-m developmeni r»/d«vu
26、t server sql develcpment lean9a clisrpath variable can be >dded to a project* x clu pith< it can be u*ed to define the location of « jar file that xrn-1 pxt of qhe orkrpace. iks modifiablestag are mecnsuly (forwcgi升 jkejjs jre-sw7. w jke.srcjwt “pwl 5 tm jke fett xn(hdrfined filurpmh vari
27、ables:ptvkit.hqle - i'sxecl iprexp2ucnfqr(, dbvit_2.2,0.:buhiy_src_h)be d:eclipreplucinjorc. dlxmit.2.eclirssjime (non bodifi&ble) - d:eclip2fi二cratlsjwe - ti:(rtilr-l.o-n:l cboovt.eclipse_»9e - d:eclit>xej>lucitx*otc. ccdelm jr£.l】b (non modifiabl deprec&t«d> - d
28、:proa»u jp遷加 <nc-n iu>(hmk <stf>rtested) - d:pipci xajee.srcroot (rvon aodifiabl deprecated) (enpty>' jumit.boe gvx» bodifikble deprecited - d:ecli;jtwit.src.hie (now ao4ifi>hlef depreeatedl - t>:redit variable entryrhmm: grails.hjie然后還需要禁用groovy插件的class auto gener
29、ate的功能,在contact上右鍵> properties> groovy project properties > 選擇 disable groovy compiler generating class files > 點(diǎn)擊ok:xtx xtz xtz xtz %lzkl kl kl kl kix kix xtz kjx kixtz kix kix kixklslz*卜#卜“廣b卜"卜"卜“廣*卜“廣#卜*卜.卜*卜*卜"卜卜"卜"卜#卜.卜#卜.卜卜.卜#卜.卜.卜.卜"卜.卜.卜.卜卜叮、.卜彳、.卜叮
30、、卜#卜.卜卜.卜.卜叮、*卜叮、.卜叮、.卜"卜.卜叮、.卜叮、叮、叮、叮、彳、叮、叮、.卜斤、叮、叮、叮、斤、彳、叮、斤、叮、.卜葉、叮、這里注意導(dǎo)入項(xiàng)目后在項(xiàng)目中禁用disable groovy compiler generating classfiles可能會導(dǎo)致項(xiàng)目報(bào)一個spring文件的錯誤,如果報(bào)錯暫時先不禁用該功能, 之后可在eclipse的window中禁用該功能six six six six sxsix klz sax six klzsix lz klz klz klz klz klz klz klklz klz klzklz klzklz klz klzlzklz圖 12.禁用 class auto generate 接下來,配置使 web tools能夠編輯gspc打開 windows > preference > general > editors> file associations,添加 *.gsp,并關(guān)聯(lián)到 jsp editor 即可:圖13.將gsp關(guān)聯(lián)到j(luò)sp editor然后設(shè)置 content types:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 枝椏材購銷合同范本
- 科技手段在電力安全生產(chǎn)中的應(yīng)用研究
- 2025-2030年中國電動機(jī)吸塵器電機(jī)產(chǎn)業(yè)運(yùn)行狀況及發(fā)展趨勢預(yù)測報(bào)告
- 銷售軟件合同范本
- 2025-2030年中國玫瑰花行業(yè)發(fā)展現(xiàn)狀及前景趨勢分析報(bào)告
- 圓棒銷售合同范本
- 2025-2030年中國橡膠跑道市場運(yùn)營現(xiàn)狀及發(fā)展規(guī)劃分析報(bào)告
- 2025-2030年中國桐油行業(yè)運(yùn)行狀況與發(fā)展趨勢分析報(bào)告
- 2025-2030年中國無負(fù)壓變頻供水設(shè)備行業(yè)十三五規(guī)劃及投資戰(zhàn)略研究報(bào)告
- 2025-2030年中國數(shù)碼相框行業(yè)市場運(yùn)行現(xiàn)狀及發(fā)展前景趨勢分析報(bào)告
- JJF1175-2021試驗(yàn)篩校準(zhǔn)規(guī)范-(高清現(xiàn)行)
- 產(chǎn)品結(jié)構(gòu)設(shè)計(jì)概述課件
- 八年級下綜合實(shí)踐教案全套
- 胸痹心痛中醫(yī)診療方案及臨床路徑
- 第8課《山山水水》教學(xué)設(shè)計(jì)(新人教版小學(xué)美術(shù)六年級上冊)
- word 公章 模板
- 世界技能大賽PPT幻燈片課件(PPT 21頁)
- 中學(xué)生防溺水安全教育課件(PPT 44頁)
- Python程序設(shè)計(jì)ppt課件完整版
- T∕ZSQX 008-2020 建設(shè)工程全過程質(zhì)量行為導(dǎo)則
- 《腹膜透析》ppt課件
評論
0/150
提交評論