Home  >  Article  >  Java  >  PlayFramework completely implements an APP (6)

PlayFramework completely implements an APP (6)

黄舟
黄舟Original
2016-12-23 16:41:421141browse

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


${_post.title}


Return to homepage

${blogTitle}


 

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


              #{if post.previous()}
                                                                                                                   id)}">
                                                                                                                

 

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

Post a comment< /h3>

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

                                                                                                      " name="author" id="author" />

                                                                                                                 "content" id="content">

;

#{/form}



 

5. Add verification to verify that Author and Content are not empty

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);
} 




Edit form, display error

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn