您好,欢迎访问一九零五行业门户网

PlayFramework完整实现一个APP(八)

创建tag标签
1.创建model
@entity
@table(name = blog_tag)public class tag extends model implements comparable<tag> {
   public string name;    
   private tag(string name) {        this.name = name;
   }    
   public string tostring() {        return name;
   }    
   public int compareto(tag othertag) {        return name.compareto(othertag.name);
   }
   public static tag findorcreatebyname(string name) {
       tag tag = tag.find(byname, name).first();        if(tag == null) {
           tag = new tag(name);
       }        return tag;
   }}
2.post类添加tag属性
@manytomany(cascade = cascadetype.persist)public set<tag> tags;public post(user author, string title, string content) {
       this.comments = new arraylist<comment>();        this.tags = new treeset<tag>();        this.author = author;        this.title = title;        this.content = content;        this.postedat = new date();
}
3.post类添加方法
关联post和tag
public post tagitwith(string name) {
       tags.add(tag.findorcreatebyname(name));        return this;
}
返回关联指定tag的post集合
public static list<post> findtaggedwith(string... tags) {        return post.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.写测试用例
@testpublic void testtags() {        // create a new user and save it
       user bob = new user(bob@gmail.com, secret, bob).save();    
       // create a new post
       post bobpost = new post(bob, my first post, hello world).save();
       post anotherbobpost = new post(bob, hop, hello world).save();        
       // well
       assertequals(0, post.findtaggedwith(red).size());        
       // tag it now
       bobpost.tagitwith(red).tagitwith(blue).save();
       anotherbobpost.tagitwith(red).tagitwith(green).save();        
       // check
       assertequals(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类,添加方法
public static list<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();        return result;
}
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 mvc application
   postedat:       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列表
public static void listtagged(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>
效果:
以上就是playframework完整实现一个app(八)的内容。
其它类似信息

推荐信息