首页 > Java > java教程 > 正文

PlayFramework完整实现一个APP(八)

黄舟
发布: 2016-12-23 16:44:17
原创
1398人浏览过

创建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>

Lateral App
Lateral App

整理归类论文

Lateral App 28
查看详情 Lateral App

  

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>

  

 

效果:

213.png

214.png

 以上就是PlayFramework完整实现一个APP(八)的内容,更多相关内容请关注PHP中文网(www.php.cn)! 

相关标签:
app
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号