play框架入門知識分享_第1頁
play框架入門知識分享_第2頁
play框架入門知識分享_第3頁
play框架入門知識分享_第4頁
play框架入門知識分享_第5頁
已閱讀5頁,還剩132頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Good is good, but better carries it.精益求精,善益求善。play框架入門Play框架入門play環(huán)境變量配置把play的路徑添加到系統(tǒng)環(huán)境變量的PATH路徑中進入CMD環(huán)境,測試配置是否成功play創(chuàng)建一個簡單項目,UserPostCommentTag這里我們用samples-and-tests下的yabe項目來做例子。在cmd中playnewyabe進入創(chuàng)建的目錄運行playrun命令.在瀏覽器中輸入HYPERLINKhttp:/localhost:9000http:/localhost:9000查看創(chuàng)建的項目是否成功。使用playeclipsify表示把

2、項目轉(zhuǎn)換成一個ECLIPSE項目。輸入playtest表示以測試模式啟動。在瀏覽器中輸入HYPERLINKhttp:/localhost:9000/testshttp:/localhost:9000/tests表示進入測試JUNIT頁面,并可進行測試創(chuàng)建實體BEAN,play的實體BEAN使用的是JPA的實體把項目導(dǎo)入eclipse中,在models包中創(chuàng)建類,內(nèi)容如下:packagemodels;importjavax.persistence.Entity;importplay.db.jpa.Model;EntitypublicclassUserextendsModelpublicStrin

3、gemail;publicStringpassword;publicStringfullname;publicStringisAdmin;publicUser(Stringemail,Stringpassword,Stringfullname)this.email=email;this.password=password;this.fullname=fullname;關(guān)于實體中類的注解可以通過查看JPA2.0的相關(guān)文檔來進行了解.注意一點,我在創(chuàng)建的實體類中并沒有添加ID屬性,但是其實ID屬性是必須的屬性,如果我們在實體類中沒有顯示的指定ID屬性,PLAY會給我們創(chuàng)建一個默認的id屬性,這個屬

4、性的值為自動增加值。集成JUNIT單元測試在test包目錄下新建一個UserTest的測試類,繼承UnitTest類,如下:importmodels.User;importorg.junit.Test;importplay.test.UnitTest;publicclassUserTestextendsUnitTestTestpublicvoidcreateAndRetrieveUser()/添加Useruser=newUser(yh.sniaw,123456,小機);assertNotNull(user.save();/查詢條件下的所有信息,并返回第一個Usersearch=user.fin

5、d(byEmailLike,%gmail%).first();assertNotNull(search);assertEquals(123456,search.password);運行playtest在瀏覽器中輸入HYPERLINKhttp:/localhost:9000/testshttp:/localhost:9000/tests選擇UserTest點擊start按鈕測試。在User實體中編寫一個查詢的方法,并在測試類中添加一個測試方法,不需要重啟瀏覽器,直接進行測試。查看效果。publicstaticUserconnect(Stringemail,Stringpassword)retur

6、nUser.find(byEmailLikeAndPassword,%+email+%,password).first();TestpublicvoidtestConnectMethod()/添加Useruser=newUser(yh.sniaw,123456,小機);assertNotNull(user.save();/查詢assertNotNull(User.connect(gmail,123456);assertNull(User.connect(test,123456);assertNull(User.connect(yh.sniaw,aa);assertNotNull(User.co

7、nnect(yh.sniaw,123456);新建一個Post實體類,類的關(guān)系與User為多對一的關(guān)系。packagemodels;importjava.util.Date;importjavax.persistence.Entity;importjavax.persistence.Lob;importjavax.persistence.ManyToOne;importplay.db.jpa.Model;EntitypublicclassPostextendsModelpublicStringtitle;publicDatepostedAt;LobpublicStringcontent;Man

8、yToOnepublicUserauthor;publicPost(Userauthor,Stringtitle,Stringcontent)this.author=author;this.title=title;this.content=content;this.postedAt=newDate();創(chuàng)建一個測試方法:TestpublicvoidcreatePost()/添加Useruser=newUser(yh.sniaw,123456,小機);assertNotNull(user.save();Postpost=newPost(user,title,content);assertNotN

9、ull(post.save();assertEquals(1,Post.count();Listlist=Post.find(byAuthor,user).fetch();assertEquals(1,list.size();PostfirstPost=list.get(0);assertNotNull(firstPost);assertEquals(title,firstPost.title);assertEquals(content,firstPost.content);assertNotNull(firstPost.postedAt);assertEquals(user,firstPos

10、t.author);查看測試結(jié)果。新建一個Comment實體類,實體類與Post為多對一的關(guān)系。packagemodels;importjava.util.Date;importjavax.persistence.Entity;importjavax.persistence.Lob;importjavax.persistence.ManyToOne;importplay.db.jpa.Model;EntitypublicclassCommentextendsModelpublicStringauthor;LobpublicStringcontent;publicDatepostedAt;Man

11、yToOnepublicPostpost;publicComment(Postpost,Stringauthor,Stringcontent)this.post=post;this.author=author;this.content=content;this.postedAt=newDate();新建一個測試Comment方法。TestpublicvoidcreateComments()Useruser=newUser(yh.sniaw,123456,小機);assertNotNull(user.save();Postpost=newPost(user,title,content);asse

12、rtNotNull(post.save();newComment(post,author1,content1).save();newComment(post,author2,content2).save();Listlist=Comment.find(byPost,post).fetch();assertEquals(2,list.size();CommentfirstComment=list.get(0);assertEquals(author1,firstComment.author);assertNotNull(firstComment.postedAt);為Post添加一對多關(guān)系map

13、pedBy表示找到Comment類中的post對象進行反轉(zhuǎn)OneToMany(mappedBy=post,cascade=CascadeType.ALL)Setcomments;publicPost(Userauthor,Stringtitle,Stringcontent)comments=newLinkedHashSet(0);this.author=author;this.title=title;this.content=content;this.postedAt=newDate();添加測試方法:TestpublicvoidpostAddComments()Useruser=newUse

14、r(yh.sniaw,123456,小機);assertNotNull(user.save();Postpost=newPost(user,title,content);assertNotNull(post.save();ments.add(newComment(post,author1,content1);ments.add(newComment(post,author2,content2);post.save();PostfirstPost=Post.find(byTitle,title).first();assertEquals(2,firstPments.size();assertEq

15、uals(2,Comment.count();編寫視圖顯示添加在服務(wù)器啟動時需要處理的事情。如加載一些基礎(chǔ)數(shù)據(jù):在models中添加一個Bootstarp類。此類繼承Job類。packagemodels;.Job;.OnApplicationStart;importplay.test.Fixtures;OnApplicationStartpublicclassBootstarpextendsJobpublicvoidloadUsers()if(User.count()表示換行User(bob):email:bobpassword:secretfullname:BobisAdmin:trueUs

16、er(jeff):email:jeffpassword:secretfullname:Jeff以上代碼為YAML代碼,代碼語法見YAML語法說明:以上代碼表示添加一個User對象,對象名稱為bob,構(gòu)造方法中傳入了三個參數(shù)與值。創(chuàng)建一個測試方法進行測試,看看USER對象是否已經(jīng)創(chuàng)建。代碼如下:TestpublicvoidshowUser()System.out.println(User.count();Useruser=User.find(byEmail,bob).first();assertNotNull(user);System.out.println(user);通過playtest方式

17、啟動服務(wù)器,并進行測試。以上的測試好像沒有用。加載完成后我是能查詢到。通過運行環(huán)境不知道能不能行?,F(xiàn)在我們停止測試環(huán)境的運行,啟動運行環(huán)境。Playrun現(xiàn)在我們修改下應(yīng)用的默認首頁。打開controllers下的Application文件,修改index方法:publicstaticvoidindex()PostfirstPost=Post.find(orderbypostedAtdesc).first();ListpostList=Post.find(orderbypostedAtdesc).from(0).fetch(10);render(firstPost,postList);打開Ap

18、plication中index對應(yīng)的視圖文件,views/Application/index.html把代碼修改成如下內(nèi)容:#extendsmain.html/#settitle:Home/#iffirstPost$firstPost.titleBy$firstPost.author?.fullname$firstPost.postedAt.format(yyyy-MM-dd)|$firstPments.size()?:nocomment$firstPments.size().pluralize()#iffirstPmentslastby$firstPments.toArray()-1.aut

19、hor#/if$firstPost.content.nl2br()#ifpostList當前頁數(shù)據(jù)#listitems:postList,as:oldPost$oldPost.titleBy$oldPost.author?.fullname$oldPost.postedAt.format(yyyy-MM-dd)|$oldPments.size()comment$oldPments.size().pluralize()#ifoldPments,lastby$oldPments.toArray()-1.author#/if#/list#/if#/if#elseThereiscurrentlynot

20、hingtoreadhere.#/else注意上面代碼中的紅色部分內(nèi)容,pluralize表示結(jié)果是否大于1,如果大于返回一個s,format可以對日期進行格式化,XXXList.toArray()-1表示查詢List中最后一個值。nl2br()表示把n轉(zhuǎn)換成nl很容易看成是n1正確的是NL當然上面這樣都是groovy的語法上面提示這些東西是因為官方的tutoral中寫的方式不對,找了下groovy的語法后添加正確。創(chuàng)建自定義標簽自定義標簽可以理解為頁面模板(可在自定義標簽中寫HTML信息,并可以定義屬性,在引用標簽時把屬性傳入進來)標簽定義:在views/tags/目錄下創(chuàng)建html頁面就是

21、標簽。如:display.html標簽引用:在頁面中使用#display參數(shù):參數(shù)值/其中display為標簽的HTML名稱標簽中使用參數(shù):在標簽中使用參數(shù)以“_”開始,后面是參數(shù)名稱。標簽使用例子:定義標簽:*這是一個注釋displaypostPostandasin(full,teaser,first)*$_post.titleBy$_post.author?.fullnameCreated$_post.postedAt.format(yyyy-MM-dd)#if_as!=full|$_ments.size()?:noComment$_ments.size().pluralize()#if_

22、ments,LastBy$_ments.toArray()-1.author#/if#/if#if_as!=teaserDetail:$_post.content.nl2br()#/if#if_as=full$_ments.size()?:noComment$_ments.size().pluralize()#if_ments#listitems:_ments,as:comment$comment?.authorCreated$comment.postedAt.format(yyyy-MM-dd)Detail:$comment.content.escape().nl2br()#/list#/i

23、f#/ifindex.html頁面引用標簽:#extendsmain.html/#settitle:Home/#iffirstPost*引用自定義標簽*#displaypost:firstPost,as:first/#ifpostList當前頁數(shù)據(jù)#listitems:postList,as:oldPost*引用自定義標簽*#displaypost:oldPost,as:teaser/#/list#/if#/if#elseThereiscurrentlynothingtoreadhere.#/else測試查看效果:修改布局現(xiàn)在我們打開views/main.html頁面,修改頁面的外觀。#get

24、title/#getmoreStyles/#getmoreScripts/yabe.LogintowritesomethingAboutthisblog$blogtitle$blogbaseline#doLayout/Yabeisa(notthat)powerfulblogenginebuiltwiththeitfutureasatutorialapplication.表示式語言符號注意:上面的代碼中我們使用了:可參見HYPERLINK/documentation/1.2.4/templates#syntax/documentation/1.2.4/templates#syntax#:表示引用

25、模板(標簽)$:表示表達式:表示引用靜態(tài)資源%:使用一段腳本。&:表示引用一段消息注意,上面紅色部分引用了兩個變量值,這時候我們需要在ACTION中控制這兩個參數(shù)值。但是由于這是一個布局模塊文件,不需要在每一個控制器中重新賦值的情況下,我們可以在Application.java文件中定義一個方法,這個方法用Before來注解,表示方法在每一個動作執(zhí)行前調(diào)用。BeforestaticvoidaddDefaults()renderArgs.put(blogtitle,Play.configuration.getProperty(blog.title);renderArgs.put(blogbase

26、line,Play.configuration.getProperty(blog.baseline);上面的代碼中我們在renderArgs中存放了兩個值:bolgtitle,blogbaseline,那么這兩個值在頁面中就可以直接去使用。同時,我在方法使用了Play.configuration.getProperty方法得到配置的兩個參數(shù)值。Play.configuration表示得到conf/application.conf中配置的參數(shù)值?,F(xiàn)在我們打開application.conf文件,配置上面使用到的兩個參數(shù)值。blog.title=博客標題blog.baseline=博客基線運行pl

27、ayrun,測試下效果:從上圖我們可以看到。信息是出來了,但是還有亂碼的存在,現(xiàn)在我們處理下亂碼問題:其實這問題相對簡單,把application.conf文件修改為UTF-8編碼格式,同時打開application.conf文件時使用eclipse工具打開就不會出現(xiàn)問題。給模塊添加一些樣式,讓頁面看出來更加好看些。把HYPERLINKmain.cssmain.css文檔中的內(nèi)容復(fù)制到你的項目下面public/stylesheets目錄下的main.css文件中。通過上面的的郵件列表頁面我們學(xué)習(xí)了一定的知識,現(xiàn)在我們繼續(xù)添加郵件的查看與更新功能。在Application.java中創(chuàng)建一個sh

28、ow的方法,方法中傳入一個Post的ID屬性,publicstaticvoidshow(Longid)Postpost=Post.findById(id);render(post);在views/Application/創(chuàng)建一個與方法同名的HTML文件show.html。#extendsmain.html/*引用一個模板*#settitle:showpost/*設(shè)置模板中的參數(shù)值*#displaypost:post,as:full/*調(diào)用一個自定義標簽*修改views/tags/display.html的自定義標簽,把$_post.title替換為:$_post.title修改views/ma

29、in.html模板頁面中$blogtitle替換為:$blogtitle運行playrun查看效果。自定義URL路由規(guī)則:默認(所有)的URL路由規(guī)則都需要在conf/routes文件中配置,#Catchall*/controller/actioncontroller.action上面代碼的意思:*表示GET/POST都可以,/controller/action表示客戶端訪問路徑。controller.action表示控制器類與方法具體說明:HYPERLINK/documentation/1.2.4/routes#syntax/documentation/1.2.4/routes#syntax

30、那么通過上面的說明我們就可以定義show.html的客戶端訪問路徑。GET/posts/idApplication.show*/controller/actioncontroller.action需要把自定義放到默認的前面,否則找不到。BeforestaticvoidaddDefaults()renderArgs.put(blogtitle,Play.configuration.getProperty(blog.title);renderArgs.put(blogbaseline,Play.configuration.getProperty(blog.baseline);Mapmap=para

31、ms.data;/此處可以得到請求的參數(shù)值,并可對值進行處理注意上面的紅色部分代碼?,F(xiàn)在標記出來,在實際的項目中我們可能會用到。添加一個分頁處理的方式:A.打開實體BEANPost類,添加如下兩個方法:publicPostprevious()Postprevious=Post.find(postedAt?orderbypostedAtdesc,postedAt).first();returnnext;修改show.html頁面。在頁面后邊添加如下信息:#ifpost.previous()$post.previous().title#/if#ifpost.next()$post.next().t

32、itle#/if運行查看效果:給郵件添加回復(fù)功能:在Post實體中添加一個方法:publicvoidaddComment(Stringauthor,Stringcontent)Commentcomment=newComment(this,author,content);ments.add(comment);this.save();在Application.java中添加一個addComment方法:publicstaticvoidaddComment(Longpostid,Stringauthor,Stringcontent)Postpost=Post.findById(postid);pos

33、t.addComment(author,content);show(post.id);在show.html頁面中添加一個表單,讓用戶輸入Comment的相關(guān)信息,我們的表單定義通過play表達式來說明:Postacomment#formApplication.addComment(post.id)Yourname:Message:#/form運行,查看頁面效果,并添加一條回復(fù),查看是否可以添加成功。驗證信息當然,我們現(xiàn)在還需要給輸入添加一些驗證信息,最少不能讓用戶直接提交,得先輸入值。添加play.data.validation.*下為所有的驗證注解。HYPERLINK/documentati

34、on/1.2.4/validation/documentation/1.2.4/validation相關(guān)文檔在Application.java中修改addComment方法,publicstaticvoidaddComment(Longpostid,RequiredStringauthor,RequiredStringcontent)Postpost=Post.findById(postid);/判斷是否有錯誤,如果有,直接轉(zhuǎn)向到提交頁面,并把錯誤帶過去。if(validation.hasErrors()render(Application/show.html,post);post.addCo

35、mment(author,content);show(post.id);修改show.html頁面。#formApplication.addComment(post.id)#ifErrorsallfieldrequired#/ifErrorsYourname:Message:#/form紅色部分為為新添加的信息?,F(xiàn)在我們的錯誤消息添加完成后,一般成功的消息我們也是需要提供給用戶查看的,所以我們現(xiàn)在添加回復(fù)后的成功消息提示。修改下Application.addComment方法,添加下面代碼中的紅色部分:publicstaticvoidaddComment(Longpostid,Required

36、Stringauthor,RequiredStringcontent)Postpost=Post.findById(postid);/判斷是否有錯誤,如果有,直接轉(zhuǎn)向到提交頁面,并把錯誤帶過去。if(validation.hasErrors()render(Application/show.html,post);post.addComment(author,content);flash.success(添加信息成功,謝謝你:%s!,author);show(post.id);修改show.html,在引用display標簽前添加一個判斷是否有成功消息的地方,如果有,把消息顯示到頁面中。#iff

37、lash.success$flash.success#/if#displaypost:post,as:full/查看效果:當然我們可以對添加comment方法添加自定義的URL路由,如:修改routes文件中,在最后一行前添加一行:POST/posts/postid/commentsApplication.addComment*/controller/actioncontroller.action為提交回復(fù)信息添加一個驗證碼:通過play.libs.Images.captcha()方法來生成圖片驗證碼,為了保證生成的驗證證圖片與需要驗證的頁面認證一樣,我們需要在需要認證的頁面上提交一個唯一的值

38、到生成驗證碼圖片的方法中,通過此唯一的值把生成出來的驗證碼存放到緩存中,在需要認證的方法中通過此值得到緩存中存放的數(shù)據(jù),并判斷與輸入的值是否相同。如下,添加生成圖片驗證碼的方法:publicstaticvoidimagecode(Stringid)Images.Captchacaptcha=Images.captcha();/生成圖片字體顏色,返回生成的驗證碼Stringcode=captcha.getText(#E4EAFD);/把驗證碼存放到緩存中,通過傳入的唯一UUID值,有效期為10分鐘Cache.set(id,code,10mn);/把圖片寫入到頁面流中renderBinary(ca

39、ptcha);在顯示的show方法中生成一個UUID,并存放到頁面對象中。publicstaticvoidshow(Longid)Postpost=Post.findById(id);Stringuuid=UUID.randomUUID().toString();render(post,uuid);修改show.html頁面中,添加comment部分代碼添加驗證碼:#formApplication.addComment(post.id)#ifErrors#listitems:errors,as:error$error#/list#/ifErrorsYourname:Message:請輸入驗證碼

40、:#/form修改添加回復(fù)方法addComment方法,添加必須的認證與錯誤消息,并添加驗證碼認證。publicstaticvoidaddComment(Longpostid,Required(message=用戶名稱必須輸入)Stringauthor,Required(message=回復(fù)內(nèi)容必須輸入)Stringcontent,Required(message=驗證碼必須輸入)Stringcode,Stringuuid)Postpost=Post.findById(postid);/添加自定義驗證Stringoldcode=(String)Cache.get(uuid);System.ou

41、t.println(oldcode);validation.equals(code,oldcode).message(驗證碼輸入不正確);/判斷是否有錯誤,如果有,直接轉(zhuǎn)向到提交頁面,并把錯誤帶過去。if(validation.hasErrors()render(Application/show.html,post,uuid);post.addComment(author,content);flash.success(添加信息成功,謝謝你:%s!,author);show(post.id);運行,查看效果?,F(xiàn)在我們添加Tag實體,從最上面的關(guān)系圖我們可以看出。Post實體與Tag實體為多對多的

42、關(guān)系,所以我們還要在Post中對Tag創(chuàng)建多對多關(guān)系。EntitypublicclassTagextendsModelimplementsComparableRequiredpublicStringname;privateTag(Stringname)=name;publicStringtoString()returnname;publicintcompareTo(TagotherTag)pareTo(otherT);在Post實體類中創(chuàng)建一個Tag關(guān)系:/PERSIST表示判斷實體存不存在,如果在實體活動狀態(tài)同時數(shù)據(jù)庫存在OK,/如果實體非活動狀態(tài)同時數(shù)據(jù)庫存在會報錯,都不存在,會添加到數(shù)據(jù)

43、庫ManyToMany(cascade=CascadeType.PERSIST)publicSettags;publicPost(Userauthor,Stringtitle,Stringcontent)comments=newLinkedHashSet(0);this.author=author;this.title=title;this.content=content;this.postedAt=newDate();this.tags=newTreeSet();/TreeSet會調(diào)用類的compareTo方法排序比較添加一個通過類別名稱把類別Tag添加到Post中的方法:在Tag中添加一個

44、通過名稱查詢的方法,如果查詢不到創(chuàng)建一個新的返回:publicstaticTagfindOrCreateByName(Stringname)Tagtag=Tag.find(byName,name).first();if(tag=null)tag=newTag(name);returntag;B.在Post中添加一個添加Tag的方法:publicPosttagItWith(Stringtag)TagtagObj=Tag.findOrCreateByName(tag);this.tags.add(tagObj);returnthis;在Post中添加一個通過Tag的name屬性查詢所有Post對象

45、集合的方法:publicListfindTaggedWith(Stringtag)Listlist=Post.find(selectdistinctpfromP=?,tag).fetch();returnlist;現(xiàn)在我們在測試中創(chuàng)建一個PostTest的測試類,測試剛才的方法是否可行。importorg.junit.Test;importplay.test.UnitTest;publicclassPostTestextendsUnitTestTestpublicvoidtestpostAndTag()/CreateanewuserandsaveitUserbob=newUser(bob,se

46、cret,Bob).save();/CreateanewpostPostbobPost=newPost(bob,Myfirstpost,Helloworld).save();PostanotherBobPost=newPost(bob,Hop,Helloworld).save();/WellassertEquals(0,Post.findTaggedWith(Red).size();/TagitnowbobPost.tagItWith(Red).tagItWith(Blue).save();anotherBobPost.tagItWith(Red).tagItWith(Green).save(

47、);/CheckassertEquals(2,Post.findTaggedWith(Red).size();assertEquals(1,Post.findTaggedWith(Blue).size();assertEquals(1,Post.findTaggedWith(Green).size();以測試方式playtest方式運行應(yīng)用,測試效果:現(xiàn)在我們還有一個新的需求,傳入多個Tag的名稱,通過Tag的name屬性in,同時,通過傳入的tag.size()判斷Post引用tag的個數(shù):publicstaticListfindTaggedWith(String.tags)Listlist

48、=Post.find(selectdistinctpfromPostp+joinp.tagst+in(:tags)+groupbyp.id,p.author,p.title,p.content,p.postedAt+havingcount(t.id)=:size).bind(tags,tags).bind(size,tags.length).fetch();returnlist;添加測試方法:TestpublicvoidtestTags()/CreateanewuserandsaveitUserbob=newUser(bob,secret,Bob).save();/Createanewpost

49、PostbobPost=newPost(bob,Myfirstpost,Helloworld).save();PostanotherBobPost=newPost(bob,Hop,Helloworld).save();/WellassertEquals(0,Post.findTaggedWith(Red).size();/TagitnowbobPost.tagItWith(Red).tagItWith(Blue).save();anotherBobPost.tagItWith(Red).tagItWith(Green).save();/Check/返回1,Red有兩個Post引用assertE

50、quals(1,Post.findTaggedWith(Red,Blue).size();/返回1,Red有兩個Post引用assertEquals(1,Post.findTaggedWith(Red,Green).size();/返回0,Red只有兩個Post引用,并是我們傳入的為三個參數(shù)assertEquals(0,Post.findTaggedWith(Red,Green,Blue).size();/返回0,傳入的參數(shù)都只有一個Post引用,但是我們傳入?yún)?shù)為二。assertEquals(0,Post.findTaggedWith(Green,Blue).size();測試結(jié)果:在Tag

51、中添加一個統(tǒng)計Tag存在多個Post引用的統(tǒng)計查詢方法:publicstaticListfindTagPostCount()Listlist=Tag.find(selectnewmap(tag,count(p.id)size)fromPostp+).fetch();returnlist;添加測試方法:TestpublicvoidtestCount()Listlist=Tag.findTagPostCount();assertEquals(tag=Blue,size=1,tag=Green,size=1,tag=Red,size=2,list.toString();測試結(jié)果:此處測試不成功是因為

52、我數(shù)據(jù)庫中數(shù)據(jù)太多,現(xiàn)在我們可以在conf/init_user.yml文件中添加一些Tag的初始化數(shù)據(jù),Tag(play):name:PlayTag(architecture):name:ArchitectureTag(test):name:TestTag(mvc):name:MVCPost(jeffPost):title:TheMVCapplicationpostedAt:2009-06-06author:jefftags:-play-architecture-mvccontent:APlayOK,我們現(xiàn)在修改下display.html文件,把Post相關(guān)的tag顯示出來#if_as!=fu

53、ll|$_ments.size()?:noComment$_ments.size().pluralize()#if_ments,LastBy$_ments.toArray()-1.author#/if#/if#elseif_post.tags#listitems:_post.tags,as:tag$tag$tag_isLast?:,*從寫個Tag.toString()*#/list#/elseif紅色部分說明可以通過每次迭代的對象_isLast來判斷是否是最后一個元素運行,查詢效果?,F(xiàn)在我們接著添加一個當用戶點擊標簽時,顯示標簽對應(yīng)的POST實體信息的頁面。與控制器,在Application.

54、java中添加方法listPostByTagpublicstaticvoidlistPostByTag(Stringtag)Listlist=Post.findTaggedWith(tag);render(tag,list);配置URL路由GET/posts/idApplication.showGET/posts/tagApplication.listPostByTag從上面的配置中我們可以看出,由于原來我們配置的顯示post信息的路由與現(xiàn)在配置的tag路由相同,從路由的角度上講,路由取的是最前的配置,所以現(xiàn)在的配置方式tag基本上可以說是進不去,所以我們還需要修改下/posts/id的配置,

55、我們知道show方法中傳入的Long類型的值,所以我們可以修改下id的表達方式:修改成如下所示:GET/posts/idApplication.showGET/posts/tagApplication.listPostByTag現(xiàn)在我們添加一個listPostByTag的頁面,并添加信息:#extendsmain.html/#settitle:showtagposts+tag/#iflist.size()1Thereare$list.size()posttaggedis$tag#/if#elseiflist.size()Thereisoneposttaggedis$tag#/elseif#el

56、seThereNoposttaggedis$tag#/else*Postslist*#listitems:list,as:post#displaypost:post,as:teaser/#/list修改display.html頁面中tag的超連接引用。*從寫了Tag.toString()*$tag$tag_isLast?:,運行,查看效果。很好,連中文都已經(jīng)顯示出來了。使用play的CRUD操作:啟用crud操作。打開conf/dependencies.yml把內(nèi)容修改為如下:B.require:-play-crudC.當然,這樣還不夠,還需要把相應(yīng)的依賴模塊添加到項目中來,運行playdep

57、endencies添加依賴模塊,如果在Eclipse中開發(fā)項目,需要重新運行下playeclipsify重新導(dǎo)入相關(guān)的依賴JAR,否則會提示找不到類?,F(xiàn)在我們可以添加URL的路由功能,打開conf/routes文件,在所有控制器適配的上面添加一行,*/adminmodule:crud*/controller/actioncontroller.actionE.OK,現(xiàn)在我們就可以添加幾個控制器,現(xiàn)在我們的控制器不需要繼承Controller的類,而是去繼承CRUD類,如:我們創(chuàng)建以下幾個控制類,創(chuàng)建控制類時,一般在實體類后添加一個s表示實體類的控制器,如:Post實體類,那么他的控制器默認就應(yīng)該

58、是Posts.packagecontrollers;publicclassPostsextendsCRUDpackagecontrollers;publicclassCommentsextendsCRUDpackagecontrollers;publicclassTagsextendsCRUDpackagecontrollers;publicclassUsersextendsCRUD現(xiàn)在我們可以運行應(yīng)用,在瀏覽器中輸入我們配置的地址(admin)來訪問,會看到如下的效果:當前現(xiàn)在的顯示有點難看,因為現(xiàn)在的顯示默認都是調(diào)用了對象的toString()方法,當然,我們可以重寫toString()方

59、法來讓他們顯示更加友好。不過,我們可以通過更加友好的樣式來控制這些信息,以后的教程中會提到相應(yīng)的信息。添加CRUD的驗證信息EntitypublicclassUserextendsModelRequiredEmailpublicStringemail;RequiredpublicStringpassword;運行Users的添加操作,查詢驗證是否生效。同樣,現(xiàn)在我們可以添加Post的驗證。EntitypublicclassPostextendsModelRequiredpublicStringtitle;RequiredpublicDatepostedAt;LobRequiredMaxSize

60、(10000)publicStringcontent;ManyToOneRequiredpublicUserauthor;測試,查看效果,同樣,我們?yōu)門ag與Comment添加驗證。EntitypublicclassCommentextendsModelRequiredpublicStringauthor;LobMaxSize(10000)publicStringcontent;RequiredpublicDatepostedAt;ManyToOneRequiredpublicPostpost;RequiredpublicStringname;自定義CRUD模板頁面,CRUD中給我們定義了相當

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論