Play Framework full implementation of one app (13)

Source: Internet
Author: User

Add user Edit Area

1. Modify Admin.index ()

public static void Index () {    list<post> posts = post.find ("Author.email", security.connected ()). Fetch ();    Render (posts);}

  

2. Modify Page app/views/admin/index.html

#{extends ' admin.html '/} 

  

Admin.index.html Add Post button

<p id= "Newpost" >    <a href= "@{form ()}" ><span>+</span> Write a new post</a></p>

  

3. Perfecting the Admin.save method

 Public Static voidSave (string title, string content, string tags) {//Create PostUser author = user.find ("Byemail", security.connected ()). First (); Post Post=NewPost (author, title, content); //Set Tags list     for(String tag:tags.split ("\\s+")) {        if(Tag.trim (). Length () > 0) {Post.tags.add (Tag.findorcreatebyname (TAG)); }    }    //Validatevalidation.valid (POST); if(Validation.haserrors ()) {render ("@form", Post); }    //SavePost.save (); Index ();}

The new post can be saved

4. Modify Admin.form

public static void form (Long ID) {    if (id! = NULL) {        post post = Post.findbyid (ID);        Render (post);    }    Render ();}

  

Modify admin/index.html

#{list items:posts, as: ' Post '}    <p class= "Post ${post_parity}" >        <a href= "@{admin.form (post.id)}" >${post.title}</a>    </p>#{/list}

  

Add route

GET     /admin/myposts/{id}                 admin.formget     /admin/new                          admin.form

  

5. Modify app/views/admin/form.html

#{extends ' admin.html '/} #{ifnot post?  ID} 

  

Improved Admin.save method

 Public Static voidSave (Long ID, string title, string content, string tags) {post post; if(id = =NULL) {        //Create PostUser author = user.find ("Byemail", security.connected ()). First (); Post=NewPost (author, title, content); } Else {        //Retrieve PostPost =Post.findbyid (ID); //EditPost.title =title; Post.content=content;    Post.tags.clear (); }    //Set Tags list     for(String tag:tags.split ("\\s+")) {        if(Tag.trim (). Length () > 0) {Post.tags.add (Tag.findorcreatebyname (TAG)); }    }    //Validatevalidation.valid (POST); if(Validation.haserrors ()) {render ("@form", Post); }    //SavePost.save (); Index ();}

Add route

POST    /admin/myposts/{id}                  admin.savepost    /admin/new                           admin.save

  

。。

Play Framework full implementation of one app (13)

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.