首頁  >  文章  >  Java  >  PlayFramework完整實作一個APP(八)

PlayFramework完整實作一個APP(八)

黄舟
黄舟原創
2016-12-23 16:44:171196瀏覽

建立標籤標籤

 

1.建立模型

@Entity
@Table(name = "blog_tag")public class Tag extends Model Implements名){        this.name = name;
   }    
   public String toString() {        return name;   return name.compareTo(other)標籤.名稱);
   }
public static Tag findOrCreateByName(String name) {
       Tag tag = Tag.find("byName", name).first();        if(tag == null) {
  Tag    if(tag == null) {
  Tag name  }        return tag;
   }}


 

2.Post新增類別標籤屬性

@ManyToMany(cascade = CascadeType.PERSIST)public設定(<) ;標籤> Tags;public  使用者作者,字串標題,字串內容) {

.comments = new ArrayList();        this.tags = new TreeSet();        this.author = 作者;        this.title = 標題; ();

}




3.Post類別新增法

關聯Post與Tag

public Post tagItWith(String name) {

    

}




傳回關聯指定標籤的貼文集合

public static List; findTaggedWith(String...tags) {        return Post.find(              return Post.find(             中從不同的 p. .name in (:tags) group by p.id, p.author, p.title, p.content,p.postedAt 有count(t.id) = :size"

       ).bind("tags",tags) .bind("size",tags.length).fetch();

}




4.寫測試範例

@Testpublic void testTags() {    

@Testpublic void testTags() {    User("bob@Gmail.com", "secret", "Bob").save ();    

       // 建立一個新貼文

       Post bPost = new Post(        Post bPost = new Post(bob, "Hello world").save();

       Post anotherBobPost = new Post(bob, "Hop", "Hello world") 。節省();        
       // 嗯
       assertEquals(0, Post.findTaggedWith("Red    bobPost.tagItWith("Red").tagItWith("Blue").save ();
       anotherBobPost.tagItWith("Red").tagItWith("Green").save();        
     ).size()) ;  
       assertEquals(1, Post.findTaggedWith("紅色", "綠色").size());  
       assertEquals(0, Post.findTaggedWith,"藍色);  
       assertEquals(0, Post.findTaggedWith("綠色", "藍色").size());   }


測試Tag ; getCloud() {
       列表; result = Tag.find(            「從Post p join p.tags as t group by t.name order by t.name 選擇新標籤count(p.id) 作為磅)”
       ).f​​etch ();        return result;
}

 

/23.將標籤添加到頁面上

新增預置數據

標籤(播放):
   名稱:           播放

標籤(架構):
 名稱:          架構中🠎  名稱:          架構中🠎  名稱:         

標籤(mvc):
   名稱:           MVC

貼文(jeffPost):
標題:MVC應用程式發布:2009-06-06
作者:jeff
標籤:
-play
-Architecture-mvc
內容:&gt; aplay


7。標籤顯示






8.新增listTagged方法(應用程式控制器)

點擊Tagged,顯示所有標籤的貼文清單

public static void listTagged(String tag) { tag posts = Post.findTaggedWith(tag);

   render(tag, posts);

}

 

9.修改display.html,標籤顯示

- Tagg.標籤'}
   ${tag}${tag_isLast ? '' : ', '}
#{/list}



10.新增Route


GET     /posts/{tag}                    Application.listTededed
  /posts/{id}                             Application.show
GET     /posts/{tag }                     Application.listTagged

為{id}新增規則

GET     


11.新增貼文清單頁面,有相同標籤的貼文

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

#{extends 'main.html' /}

#{set title: '用' + 標籤標記的貼文/}


*{**** ***** 標題********* }*
#{if posts.size()>1}

有${posts.size()} 個貼文標記為${tag}

#{/if}

#{elseif posts}

   

有1 篇貼文標記為「${tag}」 '

 
#{/elseif}#{else}

沒有標示為「${tag}」的貼文

#{/else}

*{********* 貼文清單*********}*

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

#{顯示post:post, as:'teaser' /}
#{/list}






 

效果:







 以上是PlayFrameworkwork(www.27)完整的內容,更多相關內容(Pcn.Pwww. 


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn