(九)play之yabe项目【发表博文】
发表一篇博文
填充管理页面
从主页链接到管理页面时,只简单显示了登陆用户的名称
现在对显示的内容加以丰富
修改Admin中的index()
- 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,对新增内容进行显示
- #{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提交的参数
- /**
- * 返回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();
- }
配置路由
- #form
- #open a form page use get method
- GET /admin/new Admin.form
- #submit form use post method
- POST /admin/new Admin.save
编写模板-form表单
- #{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对象给页面进行显示
- <!-- 循环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 ,表示数据库中已经存在此博文,可以查询出来
用于页面的回显
- /**
- * 返回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
增加路由
- #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(),处理创建与编辑两种情况
- /**
- * 接收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参数的传递
以便后台判断是新建还是编辑已有博文
- #{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
- #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 添加验证码功能 在Application.java中添加一个action:captcha() /** * 添 ...
- (八)play之yabe项目【身份验证】
(八)play之yabe项目[身份验证] 博客分类: 框架@play framework 添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...
- (七)play之yabe项目【CRUD】
(七)play之yabe项目[CRUD] 博客分类: 框架@play framework 增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义 ...
- (三)play之yabe项目【数据模型】
(三)play之yabe项目[数据模型] 博客分类: 框架@play framework 创建项目 play new yabe What is the application name? [yab ...
- 九度OJ 1499 项目安排 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1499 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时 ...
- React(九)create-react-app创建项目 + 按需加载Ant Design
(1)create-react-app如何创建项目我前面第一章介绍过了,这里就不过多写了, (2)我们主要来说说按需加载的问题 1. 引入antd npm install antd --save 2. ...
- ElasticSearch(九):springboot项目集成消息中间件activeMQ
目的:为了将elasticsearch做成单独的服务,那么我们必须解耦,也就是业务逻辑和搜索模块是没有关系的,并且是异步的.那么项目之间通信,使用的选择有限,消息中间件是一个不错的选择. 消息中间件常 ...
- Maven的学习资料收集--(九) 构建SSH项目以及专栏maven
在这里整合一下,使用Maven构建一个SSH项目 1.新建一个Web项目 可以参照前面的博客 2.添加依赖,修改pom.xml <project xmlns="http://maven ...
随机推荐
- 【资源下载】Ext4.1.0_Doc中文版_V1.0.0_Beta正式提供下载!
*************************************************重要提示: 在2014年1月1日前一天,历时两年左右的时间,翻译小组终于完成了该API的翻译.可喜可贺 ...
- Could not get BatchedBridge, make sure your bundle is packaged correctly
react-native 运行android项目的时候运行成功但是模拟器上会提示: Could not get BatchedBridge, make sure your bundle is pack ...
- 一款基于HTML5的Web 3D开发工具
在我们协助客户进行3D应用的开发过程中,客户遇到的最头疼的问题是如何在短时间内学会使用TWaver 3D引擎,以及使用TWaver 3D来创建和导入项目所需的各种3D业务模型.由于项目涵盖的行业繁多. ...
- 如何修改Windows Azure Web App的时区
Windows Azure的时区默认使用UTC,如果是VM可以直接修改,但Azure Web Apps的隔离性导致我们无法进行设置.应用系统使用DateTime.Now的地方很多,不可能做出一一修改. ...
- annotation:@Override出现The method of type must override asuperclass解决方案
原因追踪及解决办法: 1. 查阅资料发现说在jdk1.5下要使用@Override这个annotation必须保证被标注的方法来源于class而不是interface. 2. 即使自己的jdk是1.6 ...
- Eclipse卸载插件
Eclipse卸载插件 ### 本人Eclipse版本为:Eclipse Mars 1. 选择: Help -> Install New Software , 如下图:2. 点击 what is ...
- MYSQL开发性能研究——批量插入的优化措施
一.我们遇到了什么问题 在标准SQL里面,我们通常会写下如下的SQL insert语句. INSERT INTO TBL_TEST (id) VALUES(1); 很显然,在MYSQL中,这样的方 ...
- 关于导出Excel
Asp.Net 在刚毕业那会,做项目全是服务器控件.导出Excel的代码也很简单,在button触发后台事件后,后台生成一个excel文件,然后读取成字节,输出到客户端. Response.AddHe ...
- 表格CSS样式美化
1. 单像素边框CSS表格 这是一个很常用的表格样式. <!-- CSS goes in the document HEAD or added to your external styleshe ...
- Tips7:Unity中 Scene视图 和 Game视图 中 视角(Camera)的控制
选中你要改变的相机,然后点击GameObject-->Align With View 选项(快捷键Ctrl+Shift+F)使相机视角和当前Sence视图中一样 通过这样可以控制在Game视图( ...