首页 > Java > java教程 > 正文

PlayFramework完整实现一个APP(六)

黄舟
发布: 2016-12-23 16:41:42
原创
1358人浏览过

需要为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


   ${_post.title}


 

返回主页

${blogTitle}


  

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 }

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

  

show.html页面添加导航按钮


  

4.添加评论框

Application Controller添加方法postComment()

CSS3实现五点式图片放大镜
CSS3实现五点式图片放大镜

CSS3实现五点式图片放大镜是一款鼠标移至每一个小的圆圈内会自动显示一张完整的清晰图片,创意十足,很有美感。

CSS3实现五点式图片放大镜 95
查看详情 CSS3实现五点式图片放大镜

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

  

修改show.html

Post a comment



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


       
       
   


   


       
       
   


   


       
   


#{/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}
       


           All fields are required!
       


   #{/ifErrors}

   


       
       
   


   


       
       
   


   


       
   


#{/form}

  

6.优化客户提示

加载jquery的类库

 

修改Show.html

#{if Flash.success}
  

${flash.success}


#{/if}

#{display post:post, as:'full' /}

  

添加Comment成功的提示

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

  

添加路由

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

 以上就是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号