Play Framework 完整实现一个APP(六)
需要为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(六)的更多相关文章
- 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(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
随机推荐
- C#开发微信公众平台-就这么简单(附Demo)
写在前面 阅读目录: 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件响应) 示例Demo下载 后记 最近公司在做微信开发,其实就是接口开发,网上找了很多资料, ...
- c# 实现简单udp数据的发送和接收
服务端代码 static void Main(string[] args) { UdpClient client = null; string receiveString = null; byte[] ...
- Java内存模型深度解析:顺序一致性--转
原文地址:http://www.codeceo.com/article/java-memory-3.html 数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据 ...
- 1Z0-053 争议题目解析541
1Z0-053 争议题目解析541 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 541.If you use ALTER DISKGROUP ... ADD DISK and s ...
- Hadoop入门学习笔记---part2
在<Hadoop入门学习笔记---part1>中感觉自己虽然总结的比较详细,但是始终感觉有点凌乱.不够系统化,不够简洁.经过自己的推敲和总结,现在在此处概括性的总结一下,认为在准备搭建ha ...
- Android监听系统短信数据库变化-提取短信内容
由于监听系统短信广播受到权限的限制,所以很多手机可能使用这种方式没法监听广播,从而没办法获取到系统短信,所以又重新开辟一条路. Android监听系统短信数据库内容变化使用场景: 1.监听短信数据库的 ...
- Spring加载xsd引起的问题小记
前言 最近要把之前写好的监控系统加上报警功能,就是通过rpc调用发短信发邮件的服务发送报警信息.发短信发邮件的功能是通过dubbo管理提供的.自然使用这些服务就难免用到spring.而我这又是一个st ...
- jQuery-1.9.1源码分析系列(十六)ajax——响应数据处理和api整理
ajax在得到请求响应后主要会做两个处理:获取响应数据和使用类型转化器转化数据 a.获取响应数据 获取响应数据是调用ajaxHandleResponses函数来处理. ajaxHandleRespon ...
- Net设计模式实例之原型模式( Prototype Pattern)
一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...
- 【原创】HDFS介绍
一. HDFS简介 1. HDFS全称 Hadoop Distributed FileSystem,Hadoop分布式文件系统. Hadoop有一个抽象文件系统的概念,Ha ...