You need to add the function of viewing and commenting to the Blog
1. Create the viewing function
Add the show() method in application.java
public static void show(Long id) {
Post post = Post.findById( id);
render(post);
}
Create app/views/Application/show.html
#{extends 'main.html' /}
#{set title:post.title /}
#{display post:post, as:'full' /}
Add link in page template
Visit Blog
Return to homepage
2. Create routing rules
Current page URL http://localhost:9000/application /show?id=3
is obtained by parsing the rule * /{controller}/{action} {controller}.{action}
Create a new Route before
GET /posts/{id} Application. show
The access path becomes http://localhost:9000/posts/3
More routing syntax reference: http://play-framework.herokuapp.com/zh/routes#syntax
3. Add page navigation
Add method to Post class, PRevious()next()
public Post previous() {
return Post.find("postedAt < ? order by postedAt desc", postedAt).first();
)
4. Add comment box
Application Controller adds method postComment()
public static void postComment(Long postId, String author, String content ) {
Post post = Post.findById(postId);
post.addComment(author, content);
show(postId);
}
Modify show.html
" name="author" id="author" />
"content" id="content">
;
#{/form}
5. Add verification to verify that Author and Content are not empty
Post post = Post.findById(postId);
if (validation.hasErrors()) { render("Application/show.html", post);
}
post.addComment(author, content );
show(postId);
}
Edit form, display error
#{form @Application.postComment(post.id)}
#{ifErrors}
All fields are required!
6.优化客户提示
加载jquery的类库
修改Show.html
#{if Flash.success}
${flash.success}
添加Comment成功的提示
post.addComment(author, content);
flash.success("Thanks for posting %s", author);
添加路由
POST /posts/{postId}/comments Application.postComment
以上就是PlayFramework完整实现一个APP(六)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!