Play Framework implements a complete APP (8), frameworkapp
Create a Tag
1. Create a 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. Add Tag attributes for the Post class
@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 class Addition Method
Associate Post and Tag
public Post tagItWith(String name) { tags.add(Tag.findOrCreateByName(name)); return this;}
Returns the Post set associated with the specified Tag.
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. Write Test Cases
@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()); }
Test Tag
5. Continue to modify the Tag class and add the Method
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. Add the Tag to the page
/Yabe/conf/initial-data.ymlAdd preset data
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.modify display.html to display the 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. AddListTaggedMethod (Application Controller)
Click Tagged to display all Post lists with tags
public static void listTagged(String tag) { List<Post> posts = Post.findTaggedWith(tag); render(tag, posts);}
9.modify display.html and display tags
- Tagged #{list items:_post.tags, as:'tag'} <a href="@{Application.listTagged(tag.name)}">${tag}</a>${tag_isLast ? '' : ', '}#{/list}
10. Add a Route
GET /posts/{tag} Application.listTagged
There are currently two Route rule URLs that cannot be differentiated
GET /posts/{id} Application.showGET /posts/{tag} Application.listTagged
Add Rules for {id}
GET /posts/{<[0-9]+>id} Application.show
11. Add a Post list page with the same Tag
Create/App/views/Application/listTagged.html
#{extends 'main.html' /}#{set title:'Posts tagged with ' + tag /}*{********* Title ********* }*#{if posts.size()>1}
Effect:
..