标签:
添加用户编辑区
1.修改Admin.index()
public static void index() { List<Post> posts = Post.find("author.email", Security.connected()).fetch(); render(posts); }
2.修改页面 app/views/Admin/index.html
#{extends ‘admin.html‘ /} <h3>Welcome ${user}, <span>you have written ${posts.size() ?: ‘no‘} ${posts.pluralize(‘post‘, ‘posts‘)} so far</span></h3> #{list items:posts, as:‘post‘} <p class="post ${post_parity}"> <a href="#">${post.title}</a> </p> #{/list} <p id="newPost" > <a href="#"><span>+</span> write a new post</a> </p>
个人展示页完成
3.添加新增Post页面
Admin新增Action
public static void form() { render(); } public static void save() { // Not implemented yet }
添加路由
GET /admin/new Admin.form POST /admin/new Admin.save
创建页面 /app/views/Admin/form.html
#{extends ‘admin.html‘ /} <h3>Write, <span>a new post</span></h3> #{form @save()} #{ifErrors} <p class="error"> Please correct these errors. </p> #{/ifErrors} <p> #{field ‘title‘} <label>Post title:</label> <input type="text" name="${field.name}" value="${post?.title}" class="${field.errorClass}" /> #{/field} </p> <p> #{field ‘content‘} <label>Write here:</label> <textarea name="${field.name}" class="${field.errorClass}">${post?.content}</textarea> #{/field} </p> <p> #{field ‘tags‘} <label>Enter some tags:</label> <input type="text" size="50" name="${field.name}" value="${post?.tags?.join(‘ ‘)}" /> #{/field} </p> <p> <input type="submit" value="Publish this post to the blog" /> </p> #{/form}
Admin.index.html 添加Post按钮
<p id="newPost" > <a href="@{form()}"><span>+</span> write a new post</a> </p>
3.完善Admin.Save方法
public static void save(String title, String content, String tags) { // Create post User author = User.find("byEmail", Security.connected()).first(); Post post = new Post(author, title, content); // Set tags list for(String tag : tags.split("\\s+")) { if(tag.trim().length() > 0) { post.tags.add(Tag.findOrCreateByName(tag)); } } // Validate validation.valid(post); if(validation.hasErrors()) { render("@form", post); } // Save post.save(); index(); }
新建Post可以保存了
4.修改Admin.form
public static void form(Long id) { if(id != null) { Post post = Post.findById(id); render(post); } render(); }
修改Admin/index.html
#{list items:posts, as:‘post‘} <p class="post ${post_parity}"> <a href="@{Admin.form(post.id)}">${post.title}</a> </p> #{/list}
添加Route
GET /admin/myPosts/{id} Admin.form GET /admin/new Admin.form
5.修改 app/views/Admin/form.html
#{extends ‘admin.html‘ /} #{ifnot post?.id} <h3>Write, <span>a new post</span></h3> #{/ifnot} #{else} <h3>Edit, <span>this post</span></h3> #{/else} #{form @save(post?.id)} #{ifErrors} <p class="error"> Please correct these errors. </p> #{/ifErrors} <p> #{field ‘title‘} <label>Post title:</label> <input type="text" name="${field.name}" value="${post?.title}" class="${field.errorClass}" /> #{/field} </p> <p> #{field ‘content‘} <label>Write here:</label> <textarea name="${field.name}" class="${field.errorClass}">${post?.content}</textarea> #{/field} </p> <p> #{field ‘tags‘} <label>Enter some tags:</label> <input type="text" size="50" name="${field.name}" value="${post?.tags?.join(‘ ‘)}" /> #{/field} </p> <p> <input type="submit" value="Publish this post to the blog" /> </p> #{/form}
改进Admin.Save方法
public static void save(Long id, String title, String content, String tags) { Post post; if(id == null) { // Create post User author = User.find("byEmail", Security.connected()).first(); post = new Post(author, title, content); } else { // Retrieve post post = Post.findById(id); // Edit post.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)); } } // Validate validation.valid(post); if(validation.hasErrors()) { render("@form", post); } // Save post.save(); index(); }
添加Route
POST /admin/myPosts/{id} Admin.save POST /admin/new Admin.save
。。
标签:
原文地址:http://www.cnblogs.com/alex09/p/4923406.html