play框架入门知识分享_第1页
play框架入门知识分享_第2页
play框架入门知识分享_第3页
play框架入门知识分享_第4页
play框架入门知识分享_第5页
已阅读5页,还剩132页未读 继续免费阅读

下载本文档

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

文档简介

1、Good is good, but better carries it.精益求精,善益求善。play框架入门Play框架入门play环境变量配置把play的路径添加到系统环境变量的PATH路径中进入CMD环境,测试配置是否成功play创建一个简单项目,UserPostCommentTag这里我们用samples-and-tests下的yabe项目来做例子。在cmd中playnewyabe进入创建的目录运行playrun命令.在浏览器中输入HYPERLINKhttp:/localhost:9000http:/localhost:9000查看创建的项目是否成功。使用playeclipsify表示把

2、项目转换成一个ECLIPSE项目。输入playtest表示以测试模式启动。在浏览器中输入HYPERLINKhttp:/localhost:9000/testshttp:/localhost:9000/tests表示进入测试JUNIT页面,并可进行测试创建实体BEAN,play的实体BEAN使用的是JPA的实体把项目导入eclipse中,在models包中创建类,内容如下: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;关于实体中类的注解可以通过查看JPA2.0的相关文档来进行了解.注意一点,我在创建的实体类中并没有添加ID属性,但是其实ID属性是必须的属性,如果我们在实体类中没有显示的指定ID属性,PLAY会给我们创建一个默认的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实体类,类的关系与User为多对一的关系。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();创建一个测试方法: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);查看测试结果。新建一个Comment实体类,实体类与Post为多对一的关系。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添加一对多关系map

13、pedBy表示找到Comment类中的post对象进行反转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();编写视图显示添加在服务器启动时需要处理的事情。如加载一些基础数据:在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,构造方法中传入了三个参数与值。创建一个测试方法进行测试,看看USER对象是否已经创建。代码如下:TestpublicvoidshowUser()System.out.println(User.count();Useruser=User.find(byEmail,bob).first();assertNotNull(user);System.out.println(user);通过playtest方式

17、启动服务器,并进行测试。以上的测试好像没有用。加载完成后我是能查询到。通过运行环境不知道能不能行。现在我们停止测试环境的运行,启动运行环境。Playrun现在我们修改下应用的默认首页。打开controllers下的Application文件,修改index方法:publicstaticvoidindex()PostfirstPost=Post.find(orderbypostedAtdesc).first();ListpostList=Post.find(orderbypostedAtdesc).from(0).fetch(10);render(firstPost,postList);打开Ap

18、plication中index对应的视图文件,views/Application/index.html把代码修改成如下内容:#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当前页数据#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注意上面代码中的红色部分内容,pluralize表示结果是否大于1,如果大于返回一个s,format可以对日期进行格式化,XXXList.toArray()-1表示查询List中最后一个值。nl2br()表示把n转换成nl很容易看成是n1正确的是NL当然上面这样都是groovy的语法上面提示这些东西是因为官方的tutoral中写的方式不对,找了下groovy的语法后添加正确。创建自定义标签自定义标签可以理解为页面模板(可在自定义标签中写HTML信息,并可以定义属性,在引用标签时把属性传入进来)标签定义:在views/tags/目录下创建html页面就是

21、标签。如:display.html标签引用:在页面中使用#display参数:参数值/其中display为标签的HTML名称标签中使用参数:在标签中使用参数以“_”开始,后面是参数名称。标签使用例子:定义标签:*这是一个注释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当前页数据#listitems:postList,as:oldPost*引用自定义标签*#displaypost:oldPost,as:teaser/#/list#/if#/if#elseThereiscurrentlynothingtoreadhere.#/else测试查看效果:修改布局现在我们打开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、模板(标签)$:表示表达式:表示引用静态资源%:使用一段脚本。&:表示引用一段消息注意,上面红色部分引用了两个变量值,这时候我们需要在ACTION中控制这两个参数值。但是由于这是一个布局模块文件,不需要在每一个控制器中重新赋值的情况下,我们可以在Application.java文件中定义一个方法,这个方法用Before来注解,表示方法在每一个动作执行前调用。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方法得到配置的两个参数值。Play.configuration表示得到conf/application.conf中配置的参数值。现在我们打开application.conf文件,配置上面使用到的两个参数值。blog.title=博客标题blog.baseline=博客基线运行pl

27、ayrun,测试下效果:从上图我们可以看到。信息是出来了,但是还有乱码的存在,现在我们处理下乱码问题:其实这问题相对简单,把application.conf文件修改为UTF-8编码格式,同时打开application.conf文件时使用eclipse工具打开就不会出现问题。给模块添加一些样式,让页面看出来更加好看些。把HYPERLINKmain.cssmain.css文档中的内容复制到你的项目下面public/stylesheets目录下的main.css文件中。通过上面的的邮件列表页面我们学习了一定的知识,现在我们继续添加邮件的查看与更新功能。在Application.java中创建一个sh

28、ow的方法,方法中传入一个Post的ID属性,publicstaticvoidshow(Longid)Postpost=Post.findById(id);render(post);在views/Application/创建一个与方法同名的HTML文件show.html。#extendsmain.html/*引用一个模板*#settitle:showpost/*设置模板中的参数值*#displaypost:post,as:full/*调用一个自定义标签*修改views/tags/display.html的自定义标签,把$_post.title替换为:$_post.title修改views/ma

29、in.html模板页面中$blogtitle替换为:$blogtitle运行playrun查看效果。自定义URL路由规则:默认(所有)的URL路由规则都需要在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;/此处可以得到请求的参数值,并可对值进行处理注意上面的红色部分代码。现在标记出来,在实际的项目中我们可能会用到。添加一个分页处理的方式: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运行查看效果:给邮件添加回复功能:在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的相关信息,我们的表单定义通过play表达式来说明:Postacomment#formApplication.addComment(post.id)Yourname:Message:#/form运行,查看页面效果,并添加一条回复,查看是否可以添加成功。验证信息当然,我们现在还需要给输入添加一些验证信息,最少不能让用户直接提交,得先输入值。添加play.data.validation.*下为所有的验证注解。HYPERLINK/documentati

34、on/1.2.4/validation/documentation/1.2.4/validation相关文档在Application.java中修改addComment方法,publicstaticvoidaddComment(Longpostid,RequiredStringauthor,RequiredStringcontent)Postpost=Post.findById(postid);/判断是否有错误,如果有,直接转向到提交页面,并把错误带过去。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红色部分为为新添加的信息。现在我们的错误消息添加完成后,一般成功的消息我们也是需要提供给用户查看的,所以我们现在添加回复后的成功消息提示。修改下Application.addComment方法,添加下面代码中的红色部分:publicstaticvoidaddComment(Longpostid,Required

36、Stringauthor,RequiredStringcontent)Postpost=Post.findById(postid);/判断是否有错误,如果有,直接转向到提交页面,并把错误带过去。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为提交回复信息添加一个验证码:通过play.libs.Images.captcha()方法来生成图片验证码,为了保证生成的验证证图片与需要验证的页面认证一样,我们需要在需要认证的页面上提交一个唯一的值

38、到生成验证码图片的方法中,通过此唯一的值把生成出来的验证码存放到缓存中,在需要认证的方法中通过此值得到缓存中存放的数据,并判断与输入的值是否相同。如下,添加生成图片验证码的方法: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修改添加回复方法addComment方法,添加必须的认证与错误消息,并添加验证码认证。publicstaticvoidaddComment(Longpostid,Required(message=用户名称必须输入)Stringauthor,Required(message=回复内容必须输入)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(验证码输入不正确);/判断是否有错误,如果有,直接转向到提交页面,并把错误带过去。if(validation.hasErrors()render(Application/show.html,post,uuid);post.addComment(author,content);flash.success(添加信息成功,谢谢你:%s!,author);show(post.id);运行,查看效果。现在我们添加Tag实体,从最上面的关系图我们可以看出。Post实体与Tag实体为多对多的

42、关系,所以我们还要在Post中对Tag创建多对多关系。EntitypublicclassTagextendsModelimplementsComparableRequiredpublicStringname;privateTag(Stringname)=name;publicStringtoString()returnname;publicintcompareTo(TagotherTag)pareTo(otherT);在Post实体类中创建一个Tag关系:/PERSIST表示判断实体存不存在,如果在实体活动状态同时数据库存在OK,/如果实体非活动状态同时数据库存在会报错,都不存在,会添加到数据

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会调用类的compareTo方法排序比较添加一个通过类别名称把类别Tag添加到Post中的方法:在Tag中添加一个

44、通过名称查询的方法,如果查询不到创建一个新的返回: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;现在我们在测试中创建一个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方式运行应用,测试效果:现在我们还有一个新的需求,传入多个Tag的名称,通过Tag的name属性in,同时,通过传入的tag.size()判断Post引用tag的个数: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引用,并是我们传入的为三个参数assertEquals(0,Post.findTaggedWith(Red,Green,Blue).size();/返回0,传入的参数都只有一个Post引用,但是我们传入参数为二。assertEquals(0,Post.findTaggedWith(Green,Blue).size();测试结果:在Tag

51、中添加一个统计Tag存在多个Post引用的统计查询方法: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();测试结果:此处测试不成功是因为

52、我数据库中数据太多,现在我们可以在conf/init_user.yml文件中添加一些Tag的初始化数据,Tag(play):name:PlayTag(architecture):name:ArchitectureTag(test):name:TestTag(mvc):name:MVCPost(jeffPost):title:TheMVCapplicationpostedAt:2009-06-06author:jefftags:-play-architecture-mvccontent:APlayOK,我们现在修改下display.html文件,把Post相关的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来判断是否是最后一个元素运行,查询效果。现在我们接着添加一个当用户点击标签时,显示标签对应的POST实体信息的页面。与控制器,在Application.

54、java中添加方法listPostByTagpublicstaticvoidlistPostByTag(Stringtag)Listlist=Post.findTaggedWith(tag);render(tag,list);配置URL路由GET/posts/idApplication.showGET/posts/tagApplication.listPostByTag从上面的配置中我们可以看出,由于原来我们配置的显示post信息的路由与现在配置的tag路由相同,从路由的角度上讲,路由取的是最前的配置,所以现在的配置方式tag基本上可以说是进不去,所以我们还需要修改下/posts/id的配置,

55、我们知道show方法中传入的Long类型的值,所以我们可以修改下id的表达方式:修改成如下所示:GET/posts/idApplication.showGET/posts/tagApplication.listPostByTag现在我们添加一个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?:,运行,查看效果。很好,连中文都已经显示出来了。使用play的CRUD操作:启用crud操作。打开conf/dependencies.yml把内容修改为如下:B.require:-play-crudC.当然,这样还不够,还需要把相应的依赖模块添加到项目中来,运行playdep

57、endencies添加依赖模块,如果在Eclipse中开发项目,需要重新运行下playeclipsify重新导入相关的依赖JAR,否则会提示找不到类。现在我们可以添加URL的路由功能,打开conf/routes文件,在所有控制器适配的上面添加一行,*/adminmodule:crud*/controller/actioncontroller.actionE.OK,现在我们就可以添加几个控制器,现在我们的控制器不需要继承Controller的类,而是去继承CRUD类,如:我们创建以下几个控制类,创建控制类时,一般在实体类后添加一个s表示实体类的控制器,如:Post实体类,那么他的控制器默认就应该

58、是Posts.packagecontrollers;publicclassPostsextendsCRUDpackagecontrollers;publicclassCommentsextendsCRUDpackagecontrollers;publicclassTagsextendsCRUDpackagecontrollers;publicclassUsersextendsCRUD现在我们可以运行应用,在浏览器中输入我们配置的地址(admin)来访问,会看到如下的效果:当前现在的显示有点难看,因为现在的显示默认都是调用了对象的toString()方法,当然,我们可以重写toString()方

59、法来让他们显示更加友好。不过,我们可以通过更加友好的样式来控制这些信息,以后的教程中会提到相应的信息。添加CRUD的验证信息EntitypublicclassUserextendsModelRequiredEmailpublicStringemail;RequiredpublicStringpassword;运行Users的添加操作,查询验证是否生效。同样,现在我们可以添加Post的验证。EntitypublicclassPostextendsModelRequiredpublicStringtitle;RequiredpublicDatepostedAt;LobRequiredMaxSize

60、(10000)publicStringcontent;ManyToOneRequiredpublicUserauthor;测试,查看效果,同样,我们为Tag与Comment添加验证。EntitypublicclassCommentextendsModelRequiredpublicStringauthor;LobMaxSize(10000)publicStringcontent;RequiredpublicDatepostedAt;ManyToOneRequiredpublicPostpost;RequiredpublicStringname;自定义CRUD模板页面,CRUD中给我们定义了相当

温馨提示

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

评论

0/150

提交评论