(九)play之yabe项目【发表博文】
发表一篇博文
填充管理页面
从主页链接到管理页面时,只简单显示了登陆用户的名称
现在对显示的内容加以丰富
修改Admin中的index()
![](http://schy-hqh.iteye.com/images/icon_star.png)
- package controllers;
- import java.util.List;
- import models.Post;
- import models.User;
- import play.mvc.Before;
- import play.mvc.Controller;
- public class Admin extends Controller {
- /**
- * 首先,用户登陆会被Security拦截,登陆成功会把username放入session中
- * session.put("username",username);
- * 通过session.contains("username");判断用户是否已经登陆
- *
- * @Before 访问Admin控制器时,将先执行由该注解标注的方法,进行拦截(过滤/检查)
- */
- @Before
- static void setConnectedUser() {
- //Security.isConnected() 检查session中是否有username为key的map存在
- //因为用户登陆后会用username作为key存储登陆信息
- if(Security.isConnected()) {
- //Security.connected() 取得session中以username为key的value,即用户名
- User user = User.find("byUsername", Security.connected()).first();
- renderArgs.put("user", user.fullname);
- }
- }
- /**
- * 返回管理CRUD功能模块的主页面
- * 查询属于当前登陆用户的博文
- * 传递到admin/index.html页面进行显示
- */
- public static void index(){
- String username = Security.connected();
- //Post对象的author属性为User类型
- List<Post> posts = Post.find("author.username", username).<Post>fetch();
- render(posts);
- }
- }
同样,编辑yabe\app\views\Admin\index.html,对新增内容进行显示
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #{extends 'admin.html' /}
- <!-- 显示用户及其发布的博文数量 -->
- <h3>
- Welcome ${user},
- <span>
- you have written
- ${posts.size() ?: 'no'}
- ${posts.pluralize('post','posts')}
- so far!
- </span>
- <!-- posts.pluralizer(x,y) : posts的size为单数,则返回x,否则返回y-->
- </h3>
- <!-- 循环posts,分别显示每个博文 -->
- #{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>
进入管理页面http://localhost:9000/admin/index
显示了当前博主发布博文情况,以及发布新博文的按钮
发表一篇博文
三步:
创建Controller,或在Controller中加入action
给action配置路由
编写action对应的模板
在Admin中编写action,一个用于返回一个form表单;一个用于接收form提交的参数
![](http://schy-hqh.iteye.com/images/icon_star.png)
- /**
- * 返回form的页面
- */
- public static void form() {
- render();
- }
- /**
- * 接收form提交的数据并处理
- */
- public static void save(String title, String content) {
- //current login user
- User author = User.find("byUsername", Security.connected()).first();
- //new post
- Post post = new Post(title, content, author);
- //Validate
- validation.valid(post);
- if(validation.hasErrors()) {
- //一种简写,等效于 render("Admin/form.html",post);
- render("@form",post);
- }
- //Save object
- post.save();
- index();
- }
配置路由
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #form
- #open a form page use get method
- GET /admin/new Admin.form
- #submit form use post method
- POST /admin/new Admin.save
编写模板-form表单
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #{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}" />
- <span class="error">#{error 'post.title' /}</span>
- #{/field}
- </p>
- <p>
- #{field 'content'}
- <label>Write here:</label>
- <textarea name="${field.name}">${post?.content}</textarea>
- <span class="error">#{error 'post.content' /}</span>
- #{/field}
- </p>
- <p>
- <input type="submit" value="Publish this post to the blog" />
- </p>
- #{/form}
刷新页面,点击创建一个新的博文
修改admin/index.html,为每个博文设置链接
注意:这里需要传入博文的id,以便后台查询出该博文的所有信息,再返回Post对象给页面进行显示
![](http://schy-hqh.iteye.com/images/icon_star.png)
- <!-- 循环posts,分别显示每个博文 -->
- #{list items:posts, as:'post'}
- <p class="post ${post_parity}">
- <a href="@{Admin.form(post.id)}">${post.title}</a>
- </p>
- #{/list}
修改Admin控制器中的form(),接收id参数
如果id != null ,表示数据库中已经存在此博文,可以查询出来
用于页面的回显
![](http://schy-hqh.iteye.com/images/icon_star.png)
- /**
- * 返回form的页面
- * 初次创建博文,id为空,不返回对象
- * id不为空,则查询出对象,传递到页面用作数据回显
- */
- public static void form(Long id) {
- if(id!=null) {
- Post post = Post.findById(id);
- render(post);
- }
- render();
- }
修改路由,定制更好看的URL
http://localhost:9000/admin/new?id=6
增加路由
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #open a form page use get method
- GET /admin/myPosts/{id} Admin.form
- 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(),处理创建与编辑两种情况
![](http://schy-hqh.iteye.com/images/icon_star.png)
- /**
- * 接收form提交的数据并处理
- * 增加id参数,由页面传递过来
- * 通过id参数是否为空,判断是新建保存还是编辑保存
- */
- public static void save(Long id, String title, String content) {
- Post post = null;
- if(id==null) {
- //创建
- User author = User.find("username", controllers.Secure.Security.connected()).first();
- post = new Post(title, content, author);
- } else {
- //更新---取出已有对象,更新其内部属性并同步到数据库
- post = Post.findById(id);
- post.title = title;
- post.content = content;
- }
- //Validate
- validation.valid(post);
- if(validation.hasErrors()) {
- //一种简写,等效于 render("Admin/form.html",post);
- render("@form",post);
- }
- //Save object
- post.save();
- index();
- }
修改form.html模板,加入创建与编辑的不同显示,同时增加id参数的传递
以便后台判断是新建还是编辑已有博文
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #{extends 'admin.html' /}
- <!--
- 判断post对象id是否为空
- id==null,则新建博文
- id!=null,则编辑博文
- -->
- #{ifnot post?:id}
- <h3>Write,<span>a new post</span></h3>
- #{/if}
- #{else}
- <h3>Edit,<span>this post</span></h3>
- #{/else}
- <!-- 把id传到 Admin控制器的save action中 -->
- #{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}" />
- <span class="error">#{error 'post.title' /}</span>
- #{/field}
- </p>
- <p>
- #{field 'content'}
- <label>Write here:</label>
- <textarea name="${field.name}">${post?.content}</textarea>
- <span class="error">#{error 'post.content' /}</span>
- #{/field}
- </p>
- <p>
- <input type="submit" value="Publish this post to the blog" />
- </p>
- #{/form}
点击创建一个新的博文
点击一个已存在的博文
同样,将编辑博文的URL路径显示风格进行改变
修改routes文件
带id参数提交时的将使用第1条路由,不带参数则按第2条路由显示URL
![](http://schy-hqh.iteye.com/images/icon_star.png)
- #submit form use post method
- POST /admin/myPosts/{id} Admin.save
- POST /admin/new Admin.save
创建新的博文,保存时的URL使用第1条路由:
http://localhost:9000/admin/myPosts/1 (POST提交)
编辑已存在的博文,保存时的URL使用第2条路由:
http://localhost:9000/admin/new (POST提交)
(九)play之yabe项目【发表博文】相关推荐
- (四)play之yabe项目【页面】
(四)play之yabe项目[页面] 博客分类: 框架@play framework 主页面 显示当前发表博客的完整内容,以及历史博客列表 Bootstrap Job 一个play job任务就是一个 ...
- (八)play之yabe项目【身份验证】
(八)play之yabe项目[身份验证] 博客分类: 框架@play framework 添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改ya ...
- (七)play之yabe项目【CRUD】
(七)play之yabe项目[CRUD] 博客分类: 框架@play framework 增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义呢? ...
- 如何离线在itpub上发表博文
如何离线在itpub上发表博文 第1章 如何离线在itpub上发表博文 - 1 - 2.1 Windows Live Writer - 1 - 1.1.1 下载安装 - 1 - 1.1.2 开始配置 ...
- (三)play之yabe项目【数据模型】
(三)play之yabe项目[数据模型] 博客分类: 框架@play framework 创建项目 play new yabe What is the application name? [yabe] ...
- (十)play之yabe项目【测试】
项目测试 一个项目,首先得经过一系列的测试(开发人员自测,专门的测试人员测试,客户测试)才能上线运行. 尽早发现问题对大家都有好处,作为一个开发人员,自己写的代码最好充分自测后再提交测试. 测试从不同 ...
- (五)play之yabe项目【评论】
完成显示并发表评论功能 编写博客详细内容的页面,该页显示博客的所有评论,并可以添加新的评论! 创建显示评论的action 为了显示博文的详细页面,需要在Application.java中增加新的act ...
- 亚马逊于加拿大、美国、西班牙、瑞典和英国投资九个可再生能源新项目,成为欧洲首屈一指的可再生能源企业采购商
亚马逊现已成为欧洲乃至全球第一大可再生能源企业采购商,在世界各地有206个项目,每年采购量足以为数百万家庭供电 亚马逊在欧洲拥有超过2.5吉瓦的发电能力,在全球拥有8.5吉瓦的可再生能源发电能力,将于 ...
- (八) play之yabe项目 【身份验证】
添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改yabe\conf\dependencies.yml,加入对secure的依赖 Html代码 ...
最新文章
- 初学python还是swift-零基础如何选择编程语言 小白的我研究了3个月得出一个结论...
- Python3--自动化办公
- Python 中如何解决 asyncio 文件描述符最大数量限制问题
- 美团点评技术年货:900+页电子书,覆盖前端、后台、大数据、算法……
- 搜狐视频开放平台的申请使用方法
- 解决Springboot get请求是参数过长的情况
- 《C语言程序设计》王希杰 课后答案
- JS-元素的样式操作-文本内容-位置
- 借助计算机软件进行文学写作,网络文学创作对编辑提出的新要求及建议
- Java 八大排序算法
- 计算机网络知识点1——概述
- 软件生命周期模型汇总
- Linux查看内存的方法
- 简单的Python爬虫刷访问量程序
- HDU 5857 Median (推导)
- uchome开发入门
- linux cp: 略过目录
- U盘 未知USB设备 设定地址失败 由于该设备有问题Windows 已将其停止(代码 43) 终极解决方案(做过系统装机盘而无法解决的必看)
- 收发器(Transceiver)架构5——发信机2
- 工作站 桌面 服务器,图形工作站也虚拟化,立即让你的工作站也可以远程访问
热门文章
- 窝布靠谱-58kaopu-窝布笑话-笑话大全-唯美图片-神回复
- 让自己更优秀的 16 条法则
- MySQL 在Linux下定时备份数据库
- 【每周论文】20210110-BusTr_ConSTGAT_HetETA
- 索尼FX6摄像机视频没保存的数据恢复案例
- 【敬伟ps教程】色彩基础
- EN 14320-1保温产品为建筑设备和工业设施.现场制喷涂硬质聚氨酯(PUR)和聚异氰脲酯(PIR)泡沫--CE认证
- 用opencv把视频转为图像
- 笔记本电脑如何设置html输出,笔记本设置无线路由器步骤_怎么用笔记本设置路由器?-192路由网...
- UI设计如何确定设计风格和设计色彩?