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)