需要为Blog添加 查看和发表评论的功能

1.创建查看功能

Application.java中添加 show() 方法

public static void show(Long id) {
Post post = Post.findById(id);
render(post);
}

创建 app/views/Application/show.html

#{extends 'main.html' /}
#{set title:post.title /} #{display post:post, as:'full' /}

  

在页面模板中添加链接

访问Blog

<h2 class="post-title">
<a href="@{Application.show(_post.id)}">${_post.title}</a>
</h2>

返回主页

<h1><a href="@{Application.index()}">${blogTitle}</a></h1>

  

2.创建路由规则

当前页面URL http://localhost:9000/application/show?id=3

是由 * /{controller}/{action} {controller}.{action} 这条规则解析得到的

在之前新创建Route

GET     /posts/{id}                             Application.show

访问路径变为 http://localhost:9000/posts/3

  

更多路由语法参考: http://play-framework.herokuapp.com/zh/routes#syntax

3.添加页导航

Post类添加方法,previous()\next()

public Post previous() {
return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
} public Post next() {
return Post.find("postedAt > ? order by postedAt asc", postedAt).first();
}

  

show.html页面添加导航按钮

<ul id="pagination">
#{if post.previous()}
<li id="previous">
<a href="@{Application.show(post.previous().id)}">
${post.previous().title}
</a>
</li>
#{/if}
#{if post.next()}
<li id="next">
<a href="@{Application.show(post.next().id)}">
${post.next().title}
</a>
</li>
#{/if}
</ul>

  

4.添加评论框

Application Controller添加方法postComment()

public static void postComment(Long postId, String author, String content) {
Post post = Post.findById(postId);
post.addComment(author, content);
show(postId);
}

  

修改show.html

<h3>Post a comment</h3>

#{form @Application.postComment(post.id)}
<p>
<label for="author">Your name: </label>
<input type="text" name="author" id="author" />
</p>
<p>
<label for="content">Your message: </label>
<textarea name="content" id="content"></textarea>
</p>
<p>
<input type="submit" value="Submit your comment" />
</p>
#{/form}

  

5.添加验证,验证Author和Content非空

import play.data.validation.*;

public static void postComment(Long postId, @Required String author, @Required String content) {
Post post = Post.findById(postId);
if (validation.hasErrors()) {
render("Application/show.html", post);
}
post.addComment(author, content);
show(postId);
}  

编辑form,显示错误

#{form @Application.postComment(post.id)}

    #{ifErrors}
<p class="error">
All fields are required!
</p>
#{/ifErrors} <p>
<label for="author">Your name: </label>
<input type="text" name="author" id="author" value="${params.author}" />
</p>
<p>
<label for="content">Your message: </label>
<textarea name="content" id="content">${params.content}</textarea>
</p>
<p>
<input type="submit" value="Submit your comment" />
</p>
#{/form}

  

6.优化客户提示

加载jquery的类库

<script src="@{'/public/javascripts/jquery-1.4.2.min.js'}"></script>

<script src="@{'/public/javascripts/jquery.tools-1.2.5.toolbox.expose.min.js'}"></script>

修改Show.html

#{if flash.success} 
  <p class="success">${flash.success}</p>
#{/if} #{display post:post, as:'full' /} <script type="text/javascript" charset="utf-8">
$(function() {
// Expose the form
$('form').click(function() {
$('form').expose({api: true}).load();
}); // If there is an error, focus to form
if($('form .error').size()) {
$('form').expose({api: true, loadSpeed: 0}).load();
$('form input[type=text]').get(0).focus();
}
});
</script>

  

添加Comment成功的提示

post.addComment(author, content);
flash.success("Thanks for posting %s", author);

  

添加路由

POST    /posts/{postId}/comments                Application.postComment

  

..

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(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

随机推荐

  1. php使用post方式获得文件扩展名

    <form action="" method="post"> <input type="file" value=" ...

  2. Opencv VideoCapture实时捕捉摄像头信息

    #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv; using ...

  3. ZOJ Problem Set - 1402 Magnificent Meatballs

    比较简单的题目,题目大意就是将n个数字围成一个圈,找到一个划分,是的划分左边的数字之和等于右边的数字之和: e.g 10 1 2 2 5,那么可以找到一个划分10 | 1 2 2 5使得两边数字之和都 ...

  4. C#读取数据库中的表

    private int ExistOrNot(string name)    //判断当前数据表是否存在 { con = new SqlConnection(s); DataSet ds = new ...

  5. 深入理解javascript原型和闭包系列

    从下面目录中可以看到,本系列有16篇文章,外加两篇后补的,一共18篇文章.写了半个月,从9月17号开始写的.每篇文章更新时,读者的反馈还是可以的,虽然不至于上头条,但是也算是中规中矩,有看的人,也有评 ...

  6. VS2013预览版安装 体验截图

    支持与msdn帐号链接: 不一样的团队管理: 新建项目:

  7. jQuery-1.9.1源码分析系列(二)jQuery选择器

    1.选择器结构 jQuery的选择器根据源码可以分为几块 init: function( selector, context, rootjQuery ) { ... // HANDLE: $(&quo ...

  8. Moon.Orm版本维护及下载(跟踪报道)

    提示:最下面有最新的下载地址  历史记录: ).-- :: 支持Mysql,Sqlserver(点击下载) ).2013年9月14日,代码重构,加入oracle.复合主键功能(点击下载) )--,为d ...

  9. 一个简单的inno setup模板

    一.模板代码 基本功能包括多路径安装.多语言.自定义图标. [Setup] ShowLanguageDialog=yes AppCopyright=Copyright Reserved(C) , 36 ...

  10. javabean和jsp动作元素

    model1就是利用了jsp和javabean 的组合来处理问题.jsp页面如果有太多的逻辑代码的话,维护起来和扩展起来是相当的麻烦的.所以jsp的逻辑代码部分都打包到一种java类当中进行编写.这种 ...