Play Framework 完整实现一个APP(八)
创建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.写测试用例
- @Test
- public 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>
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>
效果:
。。
Play Framework 完整实现一个APP(八)的更多相关文章
- Play Framework 完整实现一个APP(十一)
添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...
- Play Framework 完整实现一个APP(五)
程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...
- Play Framework 完整实现一个APP(二)
1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...
- Play Framework 完整实现一个APP(十四)
添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- Play Framework 完整实现一个APP(十)
1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...
- Play Framework 完整实现一个APP(九)
添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...
- Play Framework 完整实现一个APP(六)
需要为Blog添加 查看和发表评论的功能 1.创建查看功能 Application.java中添加 show() 方法 public static void show(Long id) { Post ...
随机推荐
- linux下遍历目录
遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...
- IDDD 实现领域驱动设计-由贫血导致的失忆症
啰嗦几句 年前的时候,在和 netfocus 兄,以及对 DDD 感兴趣园友的探讨过程中,我发现自己有很多不足的地方,对 DDD 的了解也只是皮毛而已,代码写的少,DDD 的基本概念也不是很清楚,空有 ...
- 用Spire.doc来合并邮件
用Spire.doc来合并邮件 让我们想象一下这样的场景:你在一家IT公司上班.某天公司的某一产品大幅度升级了.然后你需要通知所有的客户.这真是很长的名单.一个个的通知他们是有点蠢的,因为这要花费 ...
- Oracle数据库资源管理
1.了解Resource Manager术语 2.了解Resource Manager分配方法 3.了解DEFAULT_PLAN 4.新建资源计划 5.创建使用者组 6.了解资源分配方法 7.分配使用 ...
- 自己动手丰衣足食之轮播图一动态修改marginTop属性实现轮播图
引言 学习jQuery有年头了,刚开始学习时自己动手写过轮播图,放的久了以至于忘了大致思路了.现在转而做前端,抽空把jquery轮播图拿出来写一写,把各种思路都自己练习练习,这里主要使用动态修改mar ...
- 一个简单的Webservice的demo,简单模拟服务
前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...
- 多个 App 间启动
http://developer.nokia.com/: URI associations for Windows Phone http://msdn.microsoft.com/: Auto-lau ...
- CSS基础知识汇总
前言 原文连接:http://www.cnblogs.com/wanghzh/p/5805678.html 在此基础上又做了大量的扩充 CSS简介 CSS是Cascading Style Sheets ...
- ComponentOne 2016 V2发布了!
火热的夏季迎了ComponentOne今年的第2个重大发布.这次发布包含了一些非常棒的新控件以及很多大的功能增强. 快来下载免费试用版体验吧! FlexChart(UWP.WPF.WinForms ...
- luogg_java学习_13_GUI
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! GUI 容器 JFrame , JPanel , JS ...