创建Tag标签

1.创建Model

  1. @Entity
  2. @Table(name = "blog_tag")
  3. public class Tag extends Model implements Comparable<Tag> {
  4.  
  5. public String name;
  6.  
  7. private Tag(String name) {
  8. this.name = name;
  9. }
  10.  
  11. public String toString() {
  12. return name;
  13. }
  14.  
  15. public int compareTo(Tag otherTag) {
  16. return name.compareTo(otherTag.name);
  17. }
  18.  
  19. public static Tag findOrCreateByName(String name) {
  20. Tag tag = Tag.find("byName", name).first();
  21. if(tag == null) {
  22. tag = new Tag(name);
  23. }
  24. return tag;
  25. }
  26. }

2.Post类添加Tag属性

  1. @ManyToMany(cascade = CascadeType.PERSIST)
  2. public Set<Tag> tags;
  3.  
  4. public Post(User author, String title, String content) {
  5. this.comments = new ArrayList<Comment>();
  6. this.tags = new TreeSet<Tag>();
  7. this.author = author;
  8. this.title = title;
  9. this.content = content;
  10. this.postedAt = new Date();
  11. }

 

3.Post类添加方法

关联Post和Tag

  1. public Post tagItWith(String name) {
  2. tags.add(Tag.findOrCreateByName(name));
  3. return this;
  4. }

  

返回关联指定Tag的Post集合

  1. public static List<Post> findTaggedWith(String... tags) {
  2. return Post.find(
  3. "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"
  4. ).bind("tags", tags).bind("size", tags.length).fetch();
  5. }

4.写测试用例

  1. @Test
  2. public void testTags() {
  3. // Create a new user and save it
  4. User bob = new User("bob@gmail.com", "secret", "Bob").save();
  5.  
  6. // Create a new post
  7. Post bobPost = new Post(bob, "My first post", "Hello world").save();
  8. Post anotherBobPost = new Post(bob, "Hop", "Hello world").save();
  9.  
  10. // Well
  11. assertEquals(0, Post.findTaggedWith("Red").size());
  12.  
  13. // Tag it now
  14. bobPost.tagItWith("Red").tagItWith("Blue").save();
  15. anotherBobPost.tagItWith("Red").tagItWith("Green").save();
  16.  
  17. // Check
  18. assertEquals(1, Post.findTaggedWith("Red", "Blue").size());
  19. assertEquals(1, Post.findTaggedWith("Red", "Green").size());
  20. assertEquals(0, Post.findTaggedWith("Red", "Green", "Blue").size());
  21. assertEquals(0, Post.findTaggedWith("Green", "Blue").size());
  22. }

测试Tag

5.继续修改Tag类,添加方法

  1. public static List<Map> getCloud() {
  2. List<Map> result = Tag.find(
  3. "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"
  4. ).fetch();
  5. return result;
  6. }

6.将Tag添加到页面上

/yabe/conf/initial-data.yml 添加预置数据

  1. Tag(play):
  2. name: Play
  3.  
  4. Tag(architecture):
  5. name: Architecture
  6.  
  7. Tag(test):
  8. name: Test
  9.  
  10. Tag(mvc):
  11. name: MVC
  12.  
  13. Post(jeffPost):
  14. title: The MVC application
  15. postedAt: 2009-06-06
  16. author: jeff
  17. tags:
  18. - play
  19. - architecture
  20. - mvc
  21. content: >
  22. A Play

  

7.修改display.html将tag显示出来

  1. <div class="post-metadata">
  2. <span class="post-author">by ${_post.author.fullname}</span>,
  3. <span class="post-date">${_post.postedAt.format('dd MMM yy')}</span>
  4. #{if _as != 'full'}
  5. <span class="post-comments">
  6.  |  ${_post.comments.size() ?: 'no'}
  7. comment${_post.comments.size().pluralize()}
  8. #{if _post.comments}
  9. , latest by ${_post.comments[0].author}
  10. #{/if}
  11. </span>
  12. #{/if}
  13. #{elseif _post.tags}
  14. <span class="post-tags">
  15. - Tagged
  16. #{list items:_post.tags, as:'tag'}
  17. <a href="#">${tag}</a>${tag_isLast ? '' : ', '}
  18. #{/list}
  19. </span>
  20. #{/elseif}
  21. </div>

  

8.添加listTagged 方法(Application Controller)

点击Tagged,显示所有带有Tag的Post列表

  1. public static void listTagged(String tag) {
  2. List<Post> posts = Post.findTaggedWith(tag);
  3. render(tag, posts);
  4. }

9.修改display.html,Tag显示

  1. - Tagged
  2. #{list items:_post.tags, as:'tag'}
  3. <a href="@{Application.listTagged(tag.name)}">${tag}</a>${tag_isLast ? '' : ', '}
  4. #{/list}

  

10.添加Route

  1. GET /posts/{tag} Application.listTagged

  

现在有两条Route规则URL无法区分

  1. GET /posts/{id} Application.show
  2. GET /posts/{tag} Application.listTagged

为{id}添加规则

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

  

11.添加Post list页面,有相同Tag的Post

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

  1. #{extends 'main.html' /}
  2. #{set title:'Posts tagged with ' + tag /}
  3.  
  4. *{********* Title ********* }*
  5. #{if posts.size()>1}
  6. <h3>There are ${posts.size()} posts tagged ${tag}</h3>
  7. #{/if}
  8. #{elseif posts}
  9. <h3>There is 1 post tagged '${tag}'</h3>
  10. #{/elseif}
  11. #{else}
  12. <h3>No post tagged '${tag}'</h3>
  13. #{/else}
  14.  
  15. *{********* Posts list *********}*
  16. <div class="older-posts">
  17. #{list items:posts, as:'post'}
  18. #{display post:post, as:'teaser' /}
  19. #{/list}
  20. </div>

  

效果:

  

。。

Play Framework 完整实现一个APP(八)的更多相关文章

  1. Play Framework 完整实现一个APP(十一)

    添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...

  2. Play Framework 完整实现一个APP(五)

    程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...

  3. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...

  4. Play Framework 完整实现一个APP(十四)

    添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...

  5. Play Framework 完整实现一个APP(十三)

    添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...

  6. Play Framework 完整实现一个APP(十二)

    1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...

  7. Play Framework 完整实现一个APP(十)

    1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...

  8. Play Framework 完整实现一个APP(九)

    添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...

  9. Play Framework 完整实现一个APP(六)

    需要为Blog添加 查看和发表评论的功能 1.创建查看功能 Application.java中添加 show() 方法 public static void show(Long id) { Post ...

随机推荐

  1. linux下遍历目录

    遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...

  2. IDDD 实现领域驱动设计-由贫血导致的失忆症

    啰嗦几句 年前的时候,在和 netfocus 兄,以及对 DDD 感兴趣园友的探讨过程中,我发现自己有很多不足的地方,对 DDD 的了解也只是皮毛而已,代码写的少,DDD 的基本概念也不是很清楚,空有 ...

  3. 用Spire.doc来合并邮件

      用Spire.doc来合并邮件 让我们想象一下这样的场景:你在一家IT公司上班.某天公司的某一产品大幅度升级了.然后你需要通知所有的客户.这真是很长的名单.一个个的通知他们是有点蠢的,因为这要花费 ...

  4. Oracle数据库资源管理

    1.了解Resource Manager术语 2.了解Resource Manager分配方法 3.了解DEFAULT_PLAN 4.新建资源计划 5.创建使用者组 6.了解资源分配方法 7.分配使用 ...

  5. 自己动手丰衣足食之轮播图一动态修改marginTop属性实现轮播图

    引言 学习jQuery有年头了,刚开始学习时自己动手写过轮播图,放的久了以至于忘了大致思路了.现在转而做前端,抽空把jquery轮播图拿出来写一写,把各种思路都自己练习练习,这里主要使用动态修改mar ...

  6. 一个简单的Webservice的demo,简单模拟服务

    前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...

  7. 多个 App 间启动

    http://developer.nokia.com/: URI associations for Windows Phone http://msdn.microsoft.com/: Auto-lau ...

  8. CSS基础知识汇总

    前言 原文连接:http://www.cnblogs.com/wanghzh/p/5805678.html 在此基础上又做了大量的扩充 CSS简介 CSS是Cascading Style Sheets ...

  9. ComponentOne 2016 V2发布了!

    火热的夏季迎了ComponentOne今年的第2个重大发布.这次发布包含了一些非常棒的新控件以及很多大的功能增强.   快来下载免费试用版体验吧! FlexChart(UWP.WPF.WinForms ...

  10. luogg_java学习_13_GUI

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! GUI 容器 JFrame , JPanel , JS ...