Play Framework full implementation of one app (eight)

Source: Internet
Author: User

Create tag Tag

1. Create model

@Entity @table (name="Blog_tag") Public classTag extends Model implements Comparable<tag> {      PublicString name; PrivateTag (String name) { This. Name =name; }         PublicString toString () {returnname; }         Public intcompareTo (Tag othertag) {returnName.compareto (othertag.name); }      Public Statictag Findorcreatebyname (String name) {tag tag= Tag.find ("byname", name). First (); if(Tag = =NULL) {tag=NewTag (name); }        returntag; }}

2.Post Class Add Tag Property

@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 class Add method

Associating Post and tag

 Public Post Tagitwith (String name) {        Tags.add (tag.findorcreatebyname (name));         return  This ;}

  

Returns the Post collection associated with the specified tag

 Public Static List<post> findtaggedwith (String ... tags) {        return  post.find (                "Select Distinct p from Post p joins p.tags as T where T.name in (: Tags) group by p.id, P.author, p.title, p.content,p.postedat hav ing count (t.id) =: Size "        ). Bind (" tags ", tags). bind (" Size ", tags.length). Fetch ();

4. Write Test Cases

@Test Public voidtesttags () {//Create A new user and save itUser Bob =NewUser ("[Email protected]", "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 ());   }

Test tag

5. Continue to modify the tag class and add methods

 Public Static List<map> Getcloud () {        List<Map> result = Tag.find (            "select New Map (T.name as Tag , Count (p.id) as pound) from the Post p join P.tags as T GROUP by T.name ORDER by T.name "        ). Fetch ()        ; return result;}

6. Add tag to the page

/yabe/conf/initial-data.yml Adding 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. Add listtagged method (Application Controller)

Click Tagged to display all the post lists with tags

 Public Static void listtagged (String tag) {    List<Post> posts = Post.findtaggedwith (tag);    Render (tag, posts);}

9. Modify Display.html,tag Display

-Tagged #{list items:_post.tags, as: ' Tag '}    <a href= "@{application.listtagged (Tag.name)}" >${tag}</a >${tag_islast? ': ', '}#{/list}

  

10. Add route

GET     /posts/{tag}                    application.listtagged

  

There are now two route rule URLs that cannot be distinguished

GET     /posts/{id}                             application.showget     /posts/{tag}                    application.listtagged

Add a rule for {ID}

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

  

11. Add the Post list page with the same tag post

Create /app/views/application/listtagged.html

#{extends ' main.html '/}#{set title: ' Posts tagged with ' + tag/}*{********* title *********}*#{if posts.size () >1}< H3>there ${posts.size ()} posts tagged ${tag}

  

Effect:

  

。。

Play Framework full implementation of one app (eight)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.