PlayFramework完整实现一个APP(八)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

创建Tag标签

1.创建Model

@Entity

@Table(name="blog_tag")publicclassTag extends Model implements Comparable<Tag>{publicString name;PRivateTag(String name) {this.name =name;

}publicString toString() {returnname;

}publicintcompareTo(Tag otherTag) {returnname.compareTo(otherTag.name);

}publicstaticTag findOrCreateByName(String name) {

Tag tag= Tag.find("byName", name).first();if(tag ==null) {

tag=newTag(name);

}returntag;

}}

2.Post类添加Tag属性

@ManyToMany(cascade =CascadeType.PERSIST)publicSet<Tag>tags;publicPost(User author, String title, String content) {this.comments =newArrayList<Comment>();this.tags =newTreeSet<Tag>();this.author =author;this.title =title;this.content =content;this.postedAt =newDate();

}

3.Post类添加方法

关联Post和Tag

publicPost tagItWith(String name) {

tags.add(Tag.findOrCreateByName(name));returnthis;

}

返回关联指定Tag的Post集合

publicstaticList<Post>findTaggedWith(String... tags) {returnPost.find("select distinct p from Post p join p.tags as t where t.name in (:tags) group by p.id, p.author, p.title, p.content,p.postedAt having count(t.id) = :size").bind("tags", tags).bind("size", tags.length).fetch();

}

4.写测试用例

@TestpublicvoidtestTags() {//Create a new user and save itUser bob =newUser("bob@Gmail.com", "secret", "Bob").save();//Create a new postPost bobPost =newPost(bob, "My first post", "Hello world").save();

Post anotherBobPost=newPost(bob, "Hop", "Hello world").save();//WellassertEquals(0, Post.findTaggedWith("Red").size());//Tag it nowbobPost.tagItWith("Red").tagItWith("Blue").save();

anotherBobPost.tagItWith("Red").tagItWith("Green").save();//CheckassertEquals(1, Post.findTaggedWith("Red", "Blue").size());

assertEquals(1, Post.findTaggedWith("Red", "Green").size());

assertEquals(0, Post.findTaggedWith("Red", "Green", "Blue").size());

assertEquals(0, Post.findTaggedWith("Green", "Blue").size());}

测试Tag

5.继续修改Tag类,添加方法

publicstaticList<Map>getCloud() {

List<Map> result =Tag.find("select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name order by t.name").fetch();returnresult;

}

6.将Tag添加到页面上

/yabe/conf/initial-data.yml添加预置数据

Tag(play):

name: Play

Tag(architecture):

name: Architecture

Tag(test):

name: Test

Tag(mvc):

name: MVC

Post(jeffPost):

title: The MVCapplicationpostedAt: 2009-06-06

author: jeff

tags:

- play

- architecture

- mvc

content: >

A Play

7.修改display.html将tag显示出来

<div class="post-metadata">

<span class="post-author">by ${_post.author.fullname}</span>,

<span class="post-date">${_post.postedAt.format('dd MMM yy')}</span>

#{if _as != 'full'}

<span class="post-comments">

| ${_post.comments.size() ?: 'no'}

comment${_post.comments.size().pluralize()}

#{if _post.comments}

, latest by ${_post.comments[0].author}

#{/if}

</span>

#{/if}

#{elseif _post.tags}

<span class="post-tags">

- Tagged

#{list items:_post.tags, as:'tag'}

<a href="#">${tag}</a>${tag_isLast ? '' : ', '}

#{/list}

</span>

#{/elseif}

</div>

8.添加listTagged 方法(Application Controller)

点击Tagged,显示所有带有Tag的Post列表

publicstaticvoidlistTagged(String tag) {

List<Post> posts =Post.findTaggedWith(tag);

render(tag, posts);

}

9.修改display.html,Tag显示

- Tagged

#{list items:_post.tags, as:'tag'}

<a href="@{Application.listTagged(tag.name)}">${tag}</a>${tag_isLast ? '' : ', '}

#{/list}

10.添加Route

GET /posts/{tag} Application.listTagged

现在有两条Route规则URL无法区分

GET /posts/{id} Application.show

GET /posts/{tag} Application.listTagged

为{id}添加规则

GET /posts/{<[0-9]+>id} Application.show

11.添加Post list页面,有相同Tag的Post

创建/app/views/Application/listTagged.html

#{extends 'main.html' /}

#{set title:'Posts tagged with ' + tag /}

*{********* Title ********* }*

#{if posts.size()>1}

<h3>There are ${posts.size()} posts tagged ${tag}</h3>

#{/if}

#{elseif posts}

<h3>There is 1 post tagged '${tag}'</h3>

#{/elseif}

#{else}

<h3>No post tagged '${tag}'</h3>

#{/else}

*{********* Posts list *********}*

<div class="older-posts">

#{list items:posts, as:'post'}

#{display post:post, as:'teaser' /}

#{/list}

</div>

效果:

。。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航