搜索
首页Javajava教程PlayFramework完整实现一个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


   ${_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 < ? order by postedAt desc", postedAt).first();
}

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

  

show.html页面添加导航按钮


  

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

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)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)