发表一篇博文

填充管理页面

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

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

修改Admin中的index()

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

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

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

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

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

发表一篇博文

三步:

创建Controller,或在Controller中加入action

给action配置路由

编写action对应的模板

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

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

配置路由

  1. #form
  2. #open a form page use get method
  3. GET     /admin/new      Admin.form
  4. #submit form use post method
  5. POST            /admin/new      Admin.save

编写模板-form表单

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

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

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

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

  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 ,表示数据库中已经存在此博文,可以查询出来

用于页面的回显

  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

增加路由

  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(),处理创建与编辑两种情况

  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. //Validate
  19. validation.valid(post);
  20. if(validation.hasErrors()) {
  21. //一种简写,等效于 render("Admin/form.html",post);
  22. render("@form",post);
  23. }
  24. //Save object
  25. post.save();
  26. index();
  27. }

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

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

  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. <!-- 把id传到 Admin控制器的save action中 -->
  14. #{form @save(post?.id)}
  15. #{ifErrors}
  16. <p class="error">
  17. Please correct these errors.
  18. </p>
  19. #{/ifErrors}
  20. <p>
  21. #{field 'title'}
  22. <label>Post title:</label>
  23. <input type="text" name="${field.name}" value="${post?.title}" />
  24. <span class="error">#{error 'post.title' /}</span>
  25. #{/field}
  26. </p>
  27. <p>
  28. #{field 'content'}
  29. <label>Write here:</label>
  30. <textarea name="${field.name}">${post?.content}</textarea>
  31. <span class="error">#{error 'post.content' /}</span>
  32. #{/field}
  33. </p>
  34. <p>
  35. <input type="submit" value="Publish this post to the blog" />
  36. </p>
  37. #{/form}

点击创建一个新的博文

点击一个已存在的博文

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

修改routes文件

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

  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提交)

(九)play之yabe项目【发表博文】的更多相关文章

  1. (四)play之yabe项目【页面】

    (四)play之yabe项目[页面] 博客分类: 框架@play framework   主页面 显示当前发表博客的完整内容,以及历史博客列表 Bootstrap Job 一个play job任务就是 ...

  2. (六)play之yabe项目【验证码】

    (六)play之yabe项目[验证码] 博客分类: 框架@play framework   添加验证码功能 在Application.java中添加一个action:captcha() /** * 添 ...

  3. (八)play之yabe项目【身份验证】

    (八)play之yabe项目[身份验证] 博客分类: 框架@play framework   添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...

  4. (七)play之yabe项目【CRUD】

    (七)play之yabe项目[CRUD] 博客分类: 框架@play framework    增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义 ...

  5. (三)play之yabe项目【数据模型】

    (三)play之yabe项目[数据模型] 博客分类: 框架@play framework   创建项目 play new yabe What is the application name? [yab ...

  6. 九度OJ 1499 项目安排 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1499 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时 ...

  7. React(九)create-react-app创建项目 + 按需加载Ant Design

    (1)create-react-app如何创建项目我前面第一章介绍过了,这里就不过多写了, (2)我们主要来说说按需加载的问题 1. 引入antd npm install antd --save 2. ...

  8. ElasticSearch(九):springboot项目集成消息中间件activeMQ

    目的:为了将elasticsearch做成单独的服务,那么我们必须解耦,也就是业务逻辑和搜索模块是没有关系的,并且是异步的.那么项目之间通信,使用的选择有限,消息中间件是一个不错的选择. 消息中间件常 ...

  9. Maven的学习资料收集--(九) 构建SSH项目以及专栏maven

    在这里整合一下,使用Maven构建一个SSH项目 1.新建一个Web项目 可以参照前面的博客 2.添加依赖,修改pom.xml <project xmlns="http://maven ...

随机推荐

  1. 【资源下载】Ext4.1.0_Doc中文版_V1.0.0_Beta正式提供下载!

    *************************************************重要提示: 在2014年1月1日前一天,历时两年左右的时间,翻译小组终于完成了该API的翻译.可喜可贺 ...

  2. Could not get BatchedBridge, make sure your bundle is packaged correctly

    react-native 运行android项目的时候运行成功但是模拟器上会提示: Could not get BatchedBridge, make sure your bundle is pack ...

  3. 一款基于HTML5的Web 3D开发工具

    在我们协助客户进行3D应用的开发过程中,客户遇到的最头疼的问题是如何在短时间内学会使用TWaver 3D引擎,以及使用TWaver 3D来创建和导入项目所需的各种3D业务模型.由于项目涵盖的行业繁多. ...

  4. 如何修改Windows Azure Web App的时区

    Windows Azure的时区默认使用UTC,如果是VM可以直接修改,但Azure Web Apps的隔离性导致我们无法进行设置.应用系统使用DateTime.Now的地方很多,不可能做出一一修改. ...

  5. annotation:@Override出现The method of type must override asuperclass解决方案

    原因追踪及解决办法: 1. 查阅资料发现说在jdk1.5下要使用@Override这个annotation必须保证被标注的方法来源于class而不是interface. 2. 即使自己的jdk是1.6 ...

  6. Eclipse卸载插件

    Eclipse卸载插件 ### 本人Eclipse版本为:Eclipse Mars 1. 选择: Help -> Install New Software , 如下图:2. 点击 what is ...

  7. MYSQL开发性能研究——批量插入的优化措施

    一.我们遇到了什么问题 在标准SQL里面,我们通常会写下如下的SQL insert语句. INSERT INTO TBL_TEST (id) VALUES(1);   很显然,在MYSQL中,这样的方 ...

  8. 关于导出Excel

    Asp.Net 在刚毕业那会,做项目全是服务器控件.导出Excel的代码也很简单,在button触发后台事件后,后台生成一个excel文件,然后读取成字节,输出到客户端. Response.AddHe ...

  9. 表格CSS样式美化

    1. 单像素边框CSS表格 这是一个很常用的表格样式. <!-- CSS goes in the document HEAD or added to your external styleshe ...

  10. Tips7:Unity中 Scene视图 和 Game视图 中 视角(Camera)的控制

    选中你要改变的相机,然后点击GameObject-->Align With View 选项(快捷键Ctrl+Shift+F)使相机视角和当前Sence视图中一样 通过这样可以控制在Game视图( ...