Play Framework fully implements an APP (6) and frameworkapp

Source: Internet
Author: User

Play Framework fully implements an APP (6) and frameworkapp

 

You need to add a function for viewing and posting comments to a Blog.

 

1. Create a view Function

Add the show () method to Application. java.

public static void show(Long id) {    Post post = Post.findById(id);    render(post);}

 

CreateApp/views/Application/show.html 

#{extends 'main.html' /}#{set title:post.title /} #{display post:post, as:'full' /}

  

Add a link to the Page Template

Access Blog

 

Back to homepage

  

2. Create a routing rule

Current page URL http: // localhost: 9000/application/show? Id = 3

It is parsed by */{controller}/{action} {controller}. {action }.

Create a new Route

GET     /posts/{id}                             Application.show

The access path is changed to http: // localhost: 9000/posts/3.

  

For more routing syntax references: http://play-framework.herokuapp.com/zh/routes#syntax

 

3. Add page navigation

Post class addition method, 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 page add navigation button

<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. Add a comment box

Application Controller add 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

  

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. An error is displayed.

#{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. Optimize customer prompts

Load the jquery class library

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

 

Modify 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>

  

Prompt for successful Comment Addition

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

  

Add route

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

  

 

 

 

..

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.