(九)play之yabe项目【发表博文】

来源:互联网 发布:linux jdk设置jvm内存 编辑:程序博客网 时间:2024/05/18 15:27


发表一篇博文

 

填充管理页面

从主页链接到管理页面时,只简单显示了登陆用户的名称

现在对显示的内容加以丰富

修改Admin中的index()

Java代码  收藏代码
  1. package controllers;  
  2.   
  3. import java.util.List;  
  4.   
  5. import models.Post;  
  6. import models.User;  
  7. import play.mvc.Before;  
  8. import play.mvc.Controller;  
  9.   
  10. public class Admin extends Controller {  
  11.       
  12.     /** 
  13.      * 首先,用户登陆会被Security拦截,登陆成功会把username放入session中 
  14.      * session.put("username",username); 
  15.      * 通过session.contains("username");判断用户是否已经登陆 
  16.      *  
  17.      * @Before 访问Admin控制器时,将先执行由该注解标注的方法,进行拦截(过滤/检查) 
  18.      */  
  19.     @Before  
  20.     static void setConnectedUser() {  
  21.         //Security.isConnected() 检查session中是否有username为key的map存在  
  22.         //因为用户登陆后会用username作为key存储登陆信息  
  23.         if(Security.isConnected()) {  
  24.             //Security.connected() 取得session中以username为key的value,即用户名  
  25.             User user = User.find("byUsername", Security.connected()).first();  
  26.             renderArgs.put("user", user.fullname);  
  27.         }  
  28.     }  
  29.       
  30.     /** 
  31.      * 返回管理CRUD功能模块的主页面 
  32.      * 查询属于当前登陆用户的博文 
  33.      * 传递到admin/index.html页面进行显示 
  34.      */  
  35.     public static void index(){  
  36.         String username = Security.connected();  
  37.         //Post对象的author属性为User类型  
  38.         List<Post> posts = Post.find("author.username", username).<Post>fetch();  
  39.         render(posts);  
  40.     }  
  41.       
  42.       
  43.       
  44. }  

 

同样,编辑yabe\app\views\Admin\index.html,对新增内容进行显示

Html代码  收藏代码
  1. #{extends 'admin.html' /}  
  2.   
  3. <!-- 显示用户及其发布的博文数量 -->  
  4. <h3>  
  5.     Welcome ${user},  
  6.     <span>  
  7.         you have written   
  8.             ${posts.size() ?: 'no'}  
  9.                 ${posts.pluralize('post','posts')}  
  10.                     so far!  
  11.     </span>  
  12.     <!-- posts.pluralizer(x,y) : posts的size为单数,则返回x,否则返回y-->    
  13. </h3>  
  14.   
  15.   
  16. <!-- 循环posts,分别显示每个博文 -->  
  17. #{list items:posts, as:'post'}  
  18.     <p class="post ${post_parity}">  
  19.         <a href="#">${post.title}</a>  
  20.     </p>  
  21. #{/list}  
  22.   
  23.   
  24. <!-- 创建新的博文 -->  
  25. <p id="newPost">  
  26.     <a href="#"><span>+</span>write a new post</a>  
  27. </p>  

 

进入管理页面http://localhost:9000/admin/index

显示了当前博主发布博文情况,以及发布新博文的按钮



 

 

发表一篇博文

三步:

 创建Controller,或在Controller中加入action

 给action配置路由

编写action对应的模板

在Admin中编写action,一个用于返回一个form表单;一个用于接收form提交的参数

 

Java代码  收藏代码
  1. /** 
  2.  * 返回form的页面 
  3.  */  
  4. public static void form() {  
  5.     render();  
  6. }  
  7.   
  8. /** 
  9.  * 接收form提交的数据并处理 
  10.  */  
  11. public static void save(String title, String content) {  
  12.     //current login user  
  13.     User author = User.find("byUsername", Security.connected()).first();  
  14.       
  15.     //new post  
  16.     Post post = new Post(title, content, author);  
  17.       
  18.     //Validate  
  19.     validation.valid(post);  
  20.     if(validation.hasErrors()) {  
  21.         //一种简写,等效于 render("Admin/form.html",post);  
  22.         render("@form",post);  
  23.     }  
  24.       
  25.     //Save object  
  26.     post.save();  
  27.     index();  
  28. }  

 

 

配置路由

 

Html代码  收藏代码
  1. #form  
  2.   
  3. #open a form page use get method  
  4. GET     /admin/new      Admin.form  
  5.   
  6. #submit form use post method  
  7. POST            /admin/new      Admin.save  

   

 

 

编写模板-form表单

 

Html代码  收藏代码
  1. #{extends 'admin.html' /}  
  2.   
  3. <h3>Write,<span>a new post</span></h3>  
  4.   
  5. #{form @save()}  
  6.     #{ifErrors}  
  7.         <p class="error">  
  8.             Please correct these errors.  
  9.         </p>  
  10.     #{/ifErrors}  
  11.       
  12.     <p>  
  13.         #{field 'title'}  
  14.             <label>Post title:</label>  
  15.             <input type="text" name="${field.name}" value="${post?.title}" />  
  16.             <span class="error">#{error 'post.title' /}</span>  
  17.         #{/field}  
  18.     </p>  
  19.       
  20.       
  21.     <p>  
  22.         #{field 'content'}  
  23.             <label>Write here:</label>  
  24.             <textarea name="${field.name}">${post?.content}</textarea>  
  25.             <span class="error">#{error 'post.content' /}</span>  
  26.         #{/field}  
  27.     </p>  
  28.       
  29.       
  30.     <p>  
  31.         <input type="submit" value="Publish this post to the blog" />  
  32.     </p>  
  33. #{/form}  

 

刷新页面,点击创建一个新的博文



 



 

 

 

 

 

修改admin/index.html,为每个博文设置链接

注意:这里需要传入博文的id,以便后台查询出该博文的所有信息,再返回Post对象给页面进行显示

Java代码  收藏代码
  1. <!-- 循环posts,分别显示每个博文 -->  
  2. #{list items:posts, as:'post'}  
  3.     <p class="post ${post_parity}">  
  4.         <a href="@{Admin.form(post.id)}">${post.title}</a>  
  5.     </p>  
  6. #{/list}  

  

修改Admin控制器中的form(),接收id参数

如果id != null ,表示数据库中已经存在此博文,可以查询出来

用于页面的回显

Java代码  收藏代码
  1. /** 
  2.  * 返回form的页面 
  3.  * 初次创建博文,id为空,不返回对象 
  4.  * id不为空,则查询出对象,传递到页面用作数据回显 
  5.  */  
  6. public static void form(Long id) {  
  7.     if(id!=null) {  
  8.         Post post = Post.findById(id);  
  9.         render(post);  
  10.     }  
  11.     render();  
  12. }  

 

修改路由,定制更好看的URL

http://localhost:9000/admin/new?id=6

增加路由

Html代码  收藏代码
  1. #open a form page use get method  
  2. GET     /admin/myPosts/{id} Admin.form  
  3. GET     /admin/new      Admin.form  

第1条路由:如果一个id参数被提交,将使用第1条路由模式显示URL=== myPosts/6

第2条路由:如果没有id参数被提交,则使用旧的模式显示URL=== new?id=6

 

之后,URL变为了这种格式:http://localhost:9000/admin/myPosts/6

 

 

 

 

 区分创建与编辑

现在还存在一个问题,新建博文没有问题了

但是,编辑已经存在的博文,保存之后系统会自动将其作为一个新的博文进行发布,而不是更新

因此,需要在form()中加入判断逻辑:

如果已经存在id了,则更新;否则作为新的博文进行保存!

 

修改Admin控制器的save(),处理创建与编辑两种情况

Java代码  收藏代码
  1. /** 
  2.      * 接收form提交的数据并处理 
  3.      * 增加id参数,由页面传递过来 
  4.      * 通过id参数是否为空,判断是新建保存还是编辑保存 
  5.      */  
  6.     public static void save(Long id, String title, String content) {  
  7.         Post post = null;  
  8.         if(id==null) {  
  9.             //创建  
  10.             User author = User.find("username", controllers.Secure.Security.connected()).first();  
  11.             post = new Post(title, content, author);  
  12.         } else {  
  13.             //更新---取出已有对象,更新其内部属性并同步到数据库  
  14.             post = Post.findById(id);  
  15.             post.title = title;  
  16.             post.content = content;  
  17.         }  
  18.           
  19.         //Validate  
  20.         validation.valid(post);  
  21.         if(validation.hasErrors()) {  
  22.             //一种简写,等效于 render("Admin/form.html",post);  
  23.             render("@form",post);  
  24.         }  
  25.           
  26.         //Save object  
  27.         post.save();  
  28.         index();  
  29.     }  

 

 

修改form.html模板,加入创建与编辑的不同显示,同时增加id参数的传递

以便后台判断是新建还是编辑已有博文

Html代码  收藏代码
  1. #{extends 'admin.html' /}  
  2. <!--   
  3.     判断post对象id是否为空  
  4.     id==null,则新建博文  
  5.     id!=null,则编辑博文  
  6.  -->  
  7. #{ifnot post?:id}  
  8.     <h3>Write,<span>a new post</span></h3>  
  9. #{/if}  
  10. #{else}  
  11.     <h3>Edit,<span>this post</span></h3>  
  12. #{/else}  
  13.   
  14. <!-- 把id传到 Admin控制器的save action中 -->  
  15. #{form @save(post?.id)}  
  16.     #{ifErrors}  
  17.         <p class="error">  
  18.             Please correct these errors.  
  19.         </p>  
  20.     #{/ifErrors}  
  21.       
  22.     <p>  
  23.         #{field 'title'}  
  24.             <label>Post title:</label>  
  25.             <input type="text" name="${field.name}" value="${post?.title}" />  
  26.             <span class="error">#{error 'post.title' /}</span>  
  27.         #{/field}  
  28.     </p>  
  29.       
  30.       
  31.     <p>  
  32.         #{field 'content'}  
  33.             <label>Write here:</label>  
  34.             <textarea name="${field.name}">${post?.content}</textarea>  
  35.             <span class="error">#{error 'post.content' /}</span>  
  36.         #{/field}  
  37.     </p>  
  38.       
  39.       
  40.     <p>  
  41.         <input type="submit" value="Publish this post to the blog" />  
  42.     </p>  
  43. #{/form}  

 

 

点击创建一个新的博文



 

 

点击一个已存在的博文


 

同样,将编辑博文的URL路径显示风格进行改变

修改routes文件

带id参数提交时的将使用第1条路由,不带参数则按第2条路由显示URL

Java代码  收藏代码
  1. #submit form use post method  
  2. POST    /admin/myPosts/{id} Admin.save  
  3. POST    /admin/new      Admin.save  

  

 创建新的博文,保存时的URL使用第1条路由:

 http://localhost:9000/admin/myPosts/1 (POST提交)

 

 编辑已存在的博文,保存时的URL使用第2条路由:

http://localhost:9000/admin/new (POST提交)


0 0
原创粉丝点击